發表文章

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

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

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