--- tags: 初階班 --- # 初階班12/10小考題解 ### ***嚴禁抄襲題解的程式,請自己消化吸收再自己寫一遍*** ## a417. 理想氣體好好玩 這題其實應該考的就是**四捨五入到小數點後兩位** 那用法就是```fixed << setprecision(n)``` 那個```n```就是你要四捨五入到第幾位 :::spoiler code ```cpp= #include<bits/stdc++.h> using namespace std; int main() { int w, a, b, c; while (cin >> w >> a >> b >> c) { double x = (double) b * (double) c * 0.082 / (double) a; double y = (double) a * (double) b / 0.082 / (double) c; cout << w << '\n'; if (w == 1 || w == 2) cout << fixed << setprecision(2) << x << '\n'; else cout << fixed << setprecision(2) << y << '\n'; } } ``` ::: ## a616. 平方和 這題應該沒什麼好說的吧 也沒有陷阱 :::spoiler code ```cpp= #include <bits/stdc++.h> using namespace std; int main () { int n; while (cin >> n) { int ans = 0; int m; for (int i = 0; i < n; i++) { cin >> m; ans += m * m; } cout << ans << '\n'; } } ``` ::: ## a396. Cerulean Crescent's Debut 這題也要用到```fixed << setprecision(n)``` 會了應該就沒什麼問題 :::spoiler code ```cpp= #include<bits/stdc++.h> using namespace std; int main() { double a, b, c, d; while (cin >> a >> b >> c >> d) { double x = a / b; double y = (60 * c + d) / 1000; if (c == 0 && d == 0) break; else if (x == y || a == 0) cout << "可憐阿\n"; else if (x < y) cout << "Disconnected\n"; else cout << fixed << setprecision(3) << a - (y * b) << '\n'; } } ``` ::: ## a423. 光頭學英文 這題需要熟用ascii碼 然後會需要我們之前教過的```toupper``` :::spoiler code ```cpp= #include <bits/stdc++.h> using namespace std; int main () { int n; while (cin >> n) { cin.get(); for (int i = 0; i < n; i++) { string str; getline (cin,str); int a = str.length(); int d = 0; for (int b = 0; b < a; b++) { int c = (int)str[b]; if (c >= 97) d += (c - 96); else if (c >= 65 && c <= 90) d += 2 * (c - 64); } for (int e = 0; e < a; e++) str[e] = toupper(str[e]); cout << str << '\n'; cout << "Your score is : " << d + a << '\n'; } } } ``` ::: ## a659. Thue-Morse序列 這題就用紙筆去推一推 就會發現$i>3$以後 若為偶數,答案就是```101001``` 若為奇數,答案則是```010110``` 然後$i$可能會很大,所以要用```string```存 用ascii碼去判斷最後一位是不是偶數 :::spoiler code ```cpp= #include <bits/stdc++.h> using namespace std; int main () { ios::sync_with_stdio(0), cin.tie(0); string str; while (cin >> str) { if ((int)str[str.length() - 1] % 2 == 0) cout << "101001\n"; else cout << "010110\n"; } } ``` ::: ## a658. 最尛公倍數 這題其實就是梗題 $X=aN=bM$ 求$min(X),min(a),min(b)$ 那題目沒有限制$x, a, b$是不是正數 那就可以是$0$ 畢竟$0 = 0 * N = 0 * M$ 可能有人會想說那為什麼不行是負數 因為題目有說 **所有答案皆在unsigned int範圍** 那就代表答案為```非負整數``` :::spoiler code ```cpp= #include <bits/stdc++.h> using namespace std; int main () { int t; while (cin >> t) { int n, m; for (int i = 0; i < t; i++) { cin >> n >> m; cout << "0 0 0\n"; } } } ``` ::: ### *最後祝大家期末考順利,都能考到自己滿意的成績*