發表文章

目前顯示的是有「熱門文章」標籤的文章

c290. APCS 2017-0304-1秘密差

c290. APCS 2017-0304-1秘密差 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=c290 |  解題思路 : 先將答案設為0,加上奇數項,減掉偶數項(也可分別處理奇偶數的項的和再相減) |  程式碼 :  /* c290. APCS 2017-0304-1秘密差 https://zerojudge.tw/ShowProblem?problemid=c290 skyblue 2023/10/19 AC (2ms, 348KB) */ #include using namespace std; int main(){ //加速輸入輸出程式 ios::sync_with_stdio(0); cin.tie(0); string a; //用字串儲存輸入的數字 cin >> a; int ans = 0; //將答案初始值設為0 for(int i = 0; a[i]; i++){ if(i%2) ans += a[i]-'0'; //若是奇數項,加上值 else ans-= a[i]-'0'; //若是偶數項,減掉值 } ans = (ans>0)?ans:-ans; //將答案轉換為絕對值 cout << ans; return 0; }

b971 等差數列

b971 等差數列 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=b971 |  解題思路 : 如程式中註解 |  程式碼 :  /* b971 等差數列 skyblue https://zerojudge.tw/ShowProblem?problemid=b971 AC (3ms, 352KB) */ #include <bits/stdc++.h> using namespace std; int main(){ //加速輸入輸出程式 ios::sync_with_stdio(false); cin.tie(0); int a,m,d; cin >> a >> m >>d; int n = 1 + (m-a)/d; //計算總項數 for(int i=0; i<n; i++){ cout << a <<" "; a += d; //輸出1數後加上公差 } return 0; } /*範例輸入 #1 1 9 2 範例輸出 #1 1 3 5 7 9 */

i213. stack 練習

i213. stack 練習 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=i213 |  解題思路 : stack的基本運用     < stack的宣告>     stack<int> stk;            整數stack     stack<char> stk;         字元stack     stack<double> stk;     浮點數stack     <stack基本語法>     stk.top();                     取出stack中最上層的數     stk.pop();                    刪除stack中最上層的數     stk.push(x);                將x加入stack中     stk.empty();                若stack是空的回傳true,否則回傳false |  類題 :  a565  https://skyblue071181.blogspot.com/2024/02/a565-2p.html                      ...

a024.最大公因數(GCD)

圖片
a024.最大公因數(GCD) |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=a024 |  解題思路 :                例如找出75 ,30的最大公因數             (1) 畫出75*30的矩形               (2)以短邊30為邊長,切割出2個30*30的正方形,矩形變成15*30               (3)以短邊15為周長,切割出2個15*15的正方形               (4)矩形無法再切割,最大公因數為15                           例如找出68、30的最大公因數                                             (1)若x%y = 0,則y為最大公因數               (2)否則x轉成y,y轉成x%y |  程式碼 :  <基礎解> /* a024. 最大公因數(GCD) skyblue https://zerojudge.tw/ShowProblem?problemid=a024 ...

f605. 1. 購買力

f605. 1. 購買力 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=f605 |  解題思路 : 用min,max函式找出最大最小值,方便判斷相差多少 |  程式碼 :  /* f605. 1. 購買力 https://zerojudge.tw/ShowProblem?problemid=f605 skyblue AC (2ms, 348KB) */ #include <bits/stdc++.h> using namespace std; int main(){ int n, d; scanf("%d%d", &n, &d); int a,b,c; //times記錄物件數(買了多少東西) int times = 0; //total記錄總價錢 int total = 0; while(n--){ scanf("%d%d%d", &a,&b,&c); //用min,max函式找出最大最小值 int mi = min({a,b,c}); int ma = max({a,b,c}); //若最大最小值相差超過d,物件數+1,金額加三者的平均數 if(ma - mi >= d){ times ++; total += (a+b+c)/3; } } //輸出物件數及總價格 printf("%d %d", times,total); return 0; } /* 範例輸入 #1 1 3 24 27 21 範例輸出 #1 1 24 範例輸入 #2 3 4 24 33 42 51 48 60 77 77 77 範例輸出 #2 2 86 */

d066.上學去吧!

d066.上學去吧! |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=d066 |  解題思路 : if - else判斷 (如程式中註解) |  程式碼 :  /* d066. 上學去吧! https://zerojudge.tw/ShowProblem?problemid=d066 skyblue AC (3ms, 344KB) */ #include <bits/stdc++.h> using namespace std; int main(){ //hh為時,mm為分 int hh; int mm; scanf("%d%d", &hh, &mm); //8:00到17:00前一定在校 if(hh >= 8 && hh < 17){ printf("%s", "At School"); } //7:30以後才算到校 else if(hh == 7 && mm >= 30){ printf("%s", "At School"); } //若好17:00算放學 else if(hh == 17){ printf("%s", "Off School"); } else{ printf("%s", "Off School"); } return 0; }

a104.排序

a104.排序 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=a104 |  解題思路 : sort 函式 |  程式碼 :  /* a104. 排序 https://zerojudge.tw/ShowProblem?problemid=a104 sss 2023/10/3 AC (4ms, 316KB) */ #include <bits/stdc++.h> using namespace std; int main(){ int n; while(scanf("%d", &n) != EOF){ int a[1005] = {0}; for(int i=0; i<n; i++){ scanf("%d", &a[i]); } sort(a, a+n); printf("%d",a[0]); for(int i=1; i<n; i++){ printf(" %d", a[i]); } printf("\n"); } return 0; }

a034.二進位轉換

a034.二進位轉換 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=a034 |  解題思路 : 先知道十進位轉二進位的方法,開一個陣列紀錄每一位處理好的數,再從陣列的最後一個數開始輸出 |  程式碼 :  /* /* a034二進位轉換 skyblue AC (2ms, 324KB) */ #include <bits/stdc++.h> using namespace std; int main(){ int n; int a[50]; while(scanf("%d", &n) != EOF){ int i = 0; //轉換成二進位 while(n > 0){ a[i] = n%2; n /= 2; i++; } //從陣列的最後一個數開始輸出 for(int c = i-1; c>=0; c--){ printf("%d", a[c]); } printf("\n"); } return 0; }