發表文章

目前顯示的是 2月, 2024的文章

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 sss 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*/

a565. 2.p&q的邂逅

a565. 2.p&q的邂逅 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=a565 |  解題思路 : 將p放進stack中,若有q,則對數+1並將stack中最上層的p刪掉,即可計算對數                              |  類題 :  i213  https://skyblue071181.blogspot.com/2024/02/i213-stack.html |  程式碼 :  /* a565. 2.p&q的邂逅 https://zerojudge.tw/ShowProblem?problemid=a565 skyblue AC (0.3s, 9.8MB) */ #include <bits/stdc++.h> using namespace std; char line[100000005]; int main(){ int n; scanf("%d", &n); while(n--){ int pairs = 0; stack<char> stk; scanf("%s", line); int len = strlen(line); for(int i = 0; i<len; i++){ switch(line[i]){ case '.': break; case 'p': stk.push('p'); break; //看到一個p,就把它放進stack中 case 'q': if(!stk.empty()){ pairs

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                              |  程式碼 :  /* i213. stack 練習 https://zerojudge.tw/ShowProblem?problemid=i213 skyblue AC (17ms, 416KB) */ #include <bits/stdc++.h> using namespace std; int main(){ int n; scanf("%d", &n); stack<int> stk; while(n--){ int k; scanf("%d", &k); switch (k){ case 1: int x; scanf("%d", &x