發表文章

目前顯示的是有「基本運算」標籤的文章

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; }

c002. 10696 - f91

c002. 10696 - f91 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=c002 |  解題思路 : 遞迴運用 |  程式碼 :  /* c002. 10696 - f91 https://zerojudge.tw/ShowProblem?problemid=c002 skyblue AC (2ms, 340KB) */ #include <bits/stdc++.h> using namespace std; int f91(int x){ if(x >= 101) return x-10; else return f91(f91(x+11)); } int main(){ int x; while(scanf("%d", &x) == 1){ if(x == 0) break; else printf("f91(%d) = %d\n", x,f91(x)); } 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 */

a149 乘乘樂

a149 乘乘樂 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=a149 |  解題思路 : 將輸入的數字用字串儲存、計算 |  程式碼 :  /* a149 乘乘樂 skyblue https://zerojudge.tw/ShowProblem?problemid=a149 AC (2ms, 324KB) */ #include <bits/stdc++.h> using namespace std; int main() { //輸入輸出加速程式 ios::sync_with_stdio(0); cin.tie(0); int n,v; string str; cin >> n; for(int i=0; i<n; i++){ cin >> str; //將輸入的數字以字串儲存 v = 1; //相乘的結果初始值設為1 for(int i=0; i<str.length(); i++){ v *=(str[i]-'0'); //將各項相乘 轉換ASCII碼 } cout << v <<"\n"; } return 0; } /* 範例輸入 #1 3 356 123 9999 範例輸出 #1 90 6 6561*/

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; }

f071. 2. 刮刮樂 (Lottery)

f071. 2. 刮刮樂 (Lottery) |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=f071 |  解題思路 : (如程式中註解) |  注意 : 如範例測資3,27、36皆出現2次,故贏得金額為500+500+500+500 = 2000 |  程式碼 :  /* f071. 2. 刮刮樂 (Lottery) https://zerojudge.tw/ShowProblem?problemid=f071 skyblue AC (2ms, 348KB) */ #include <bits/stdc++.h> using namespace std; int main(){ //開3個陣列,分別記錄[幸運數字]、[號碼區的號碼]、[對應金額] int a[3]; int b[5]; int c[5]; //mon 為贏得的金額 int mon = 0; //分別輸入值 for(int i = 0; i<3; i++){ scanf("%d", &a[i]); } for(int i = 0; i<5; i++){ scanf("%d", &b[i]); } for(int i = 0; i<5; i++){ scanf("%d", &c[i]); } //若前兩個幸運數字與號碼區數字符合,mon += 對應金額 for(int i = 0; i<5; i++){ if(a[0] == b[i]){ mon += c[i]; } } for(int i = 0; i<5; i++){ if(a[1] == b[i]){ mon += c[i]; } } //yes代表第三個數是否有在號碼區中 //若有,yes質變為true bool ...

a147.Print it all

a147.Print it all |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=a147 |  解題思路 : 用while迴圈重複輸入n,for迴圈中,如果小於n的i值不可被7整除則輸出 |  程式碼 :  /* a147. Print it all skyblue https://zerojudge.tw/ShowProblem?problemid=a147 AC (6ms, 304KB) */ #include <bits/stdc++.h> using namespace std; int main(){ int n; while(cin >> n){ if(n>0){ for(int i=1; i<n; i++){ if(i%7 != 0){ cout << i <<" "; } } cout << "\n"; } else if(n == 0) break; } return 0; }

a053.計分程式

a053.計分程式 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=a053 |  解題思路 : 設題數為n,判斷n的區間,輸出相對應分數 |  程式碼 :  /* a053 計分程式 skyblue https://zerojudge.tw/ShowProblem?problemid=a053 AC (2ms, 356KB) */ #include <bits/stdc++.h> using namespace std; int main(){ //輸入輸出加速語法 ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; if(n<=10) cout << n*6 <<"\n"; if((n>= 11) && (n<=20)) cout << 60 + (n-10)*2 <<"\n"; if((n>= 21) && (n<=40)) cout << 80 + (n-20) <<"\n"; if(n>40) cout << 100<<"\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; }

a003. 兩光法師占卜術

 a003. 兩光法師占卜術 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=a003 |  解題思路 : 簡易if-else判斷 |  程式碼 :  /* a003. 兩光法師占卜術 https://zerojudge.tw/ShowProblem?problemid=a003 skyblue AC (11ms, 356KB) */ #include <bits/stdc++.h> using namespace std; int main(){ int m, n; scanf("%d%d", &m, &n); int s; s = (m*2 + n)%3; if(s == 0) printf("普通"); else if(s == 1) printf("吉"); else if (s == 2) printf("大吉"); return 0; }