# 18Dice解答 # C++ :::info ***看的人都是我的兒子 by 郭10*** ::: ## 2. 輸出函式 ### 2-1 測試系統 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { cout << "Hello, DICE!"; } ``` ### 2-2 Hello, World! ```cpp= #include<bits/stdc++.h> using namespace std; int main() { cout << "Hello, World!\nHello,World!"; } ``` ### 2-3 印三角形 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { for (int i = 1;i <= 5;i++) { for (int j = 0;j < i;j++) cout << '*'; cout << '\n'; } for (int i = 0;i < 6;i++) cout << '*'; cout << '\n'; for (int i = 5;i >= 1;i--) { for (int j = 0;j < i;j++) cout << '*'; cout << '\n'; } } ``` ### 2-4 不是計算 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { cout << "2/3=5\n2*3=5"; } ``` ### 2-5 來個表格 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { for (int i = 1;i < 11;i++) { cout << i << '\t' << i * i << '\t' << i * i * i << '\n'; } } ``` ## 3. 變數與指定運算子 ### 3-1 指定整數 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n = 3; cout << n; } ``` ### 3-2 加上一些形容詞 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n = 33; cout << "There are " << n << " cats."; } ``` ### 3-3 指定浮點數 ```cpp= #include<bits/stdc++.h> using namespace std; int main() { double a = 5.200002, b = 10.100001; cout << fixed << setprecision(6) << a << '\n' << b; } ``` ### 3-4 整數與浮點數 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n = 170; double w = 55.222; cout << "My height is " << n << " cm.\n"; cout << fixed << setprecision(3) << "My weight is " << w << " kg."; } ``` ### 3-5 =不是等號 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a; int b = 555; a = b; cout << a << ' ' << b; } ``` ### 3-6 自主學習 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { char a = 97; cout << a; } ``` ## 4. 輸入函式 ### 4-1 一個整數 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << n; return 0; } ``` ### 4-2 3個整數 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; cout << c << ' ' << b << ' ' << a; } ``` ### 4-3 浮點數 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { double a, b; cin >> a >> b; cout << fixed << setprecision(6) << a << '\n' << b; return 0; } ``` ### 4-4 3個浮點數 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { double a, b, c; cin >> a >> b >> c; cout << fixed << setprecision(3) << c << '\n'; cout << fixed << setprecision(2) << b << '\n'; cout << fixed << setprecision(1) << a << '\n'; } ``` ### 4-5 長方形 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; cout << a << ' ' << b << ' ' << c; } ``` ### 4-6 自主學習 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { char c; int n; cin >> n; c = n; cout << c; } ``` ## 5. 算術運算子 ### 5-1 四則運算 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; cout << a << '+' << b << '=' << a + b << '\n'; cout << a << '-' << b << '=' << a - b << '\n'; cout << a << '*' << b << '=' << a * b << '\n'; cout << a << '/' << b << '=' << a / b << '\n'; } ``` ### 5-2 餘數 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int c = a / b; int d = a % b; cout << a << '/' << b << '=' << c << "餘" << d << '\n'; cout << c + d; return 0; } ``` ### 5-3 梯形面積 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { double a, b, c; cin >> a >> b >> c; double ans = ((a + b) * c) / 2; cout << fixed << setprecision(1) << "梯形面積" << ans << "平方公分"; return 0; } ``` ### 5-4 溫度換算 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { double c; cin >> c; double f = c * 9 / 5; f += 32; cout << "華氏溫度="; cout << fixed << setprecision(1) << f; return 0; } ``` ### 5-5 三次溫度換算 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { double a, b, c; cin >> a >> b >> c; a = a * 9 / 5 + 32; b = b * 9 / 5 + 32; c = c * 9 / 5 + 32; double d[4]; d[1] = a; d[2] = b; d[3] = c; for (int i = 1;i < 4;i++) { cout << i; cout << ".華氏溫度="; cout << fixed << setprecision(1) << d[i] << '\n'; } } ``` ### 5-6 時間計算 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a = n; n = n * 95; cout << a << "個粽子共需要"; cout << n / 60 << "分"; cout << n % 60 << "秒"; } ``` ### 5-7 自主學習 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { char c; cin >> c; int a = 'A' - 'a'; c = c + a; cout << c; } ``` ## 6. 單一選擇 ### 6-1 及格嗎? ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n >= 60) cout << n << "分及格\n"; cout << "Over!"; } ``` ### 6-2 正數? ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n > 0) cout << n << "是正數\n"; cout << "Over!"; } ``` ### 6-3 偶數? ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 2 == 0) cout << n << "是偶數\n"; cout << "Over!"; } ``` ### 6-4 發現不相等 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; if (a != b) cout << a << "!=" << b << '\n'; cout << "Over!"; } ``` ### 6-5 輸出正數 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { float a; cin >> a; cout << fixed << setprecision(2) << abs(a) << '\n'; } ``` ### 6-6 自主學習 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int d[5] = {0, 2300, 800, 500, 1500}, a, b; cin >> a >> b; cout << (d[a] + d[b] > 3000 ? "超出預算" : "符合預算"); } ``` ## 7. 用if交換變數 ### 7-1 比大小 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; cout << "最大值" << max(a, b); } ``` ### 7-2 比三數大小 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; cout << max({a, b, c}) << ' ' << min({a, b, c}); } ``` ### 7-3 五數比大小 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int x = 0, n = 10000000; for (int i = 0;i < 5;i++) { int t; cin >> t; x = max(x, t); n = min(n, t); } cout << x << ' ' << n; } ``` ### 7-4 自主學習 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if ((n % 4 == 0 and n % 100 != 0) or n % 400 == 0) cout << "閏年\n"; else cout << "平年\n"; } ``` ## 8. 雙重選擇 ### 8-1 及格與否 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n >= 60) cout << n << "分及格"; else cout << n << "分不及格"; } ``` ### 8-2 奇數還是偶數? ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << n << ": "; if (n % 2) cout << "奇數"; else cout << "偶數"; } ``` ### 8-3 可以構成三角形? ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; cout << a << ' ' << b << ' ' << c << ": "; if (a + b <= c or b + c <= a or a + c <= b) cout << "不可以構成三角形"; else cout << "可以構成三角形"; } ``` ### 8-4 是否直角三角形? ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; cout << a << ' ' << b << ' ' << c << ": "; if (a * a + b * b == c * c or a * a + c * c == b * b or b * b + c * c == a * a) cout << "直角三角形"; else cout << "不是直角三角形"; } ``` ### 8-5 是否與6有關? ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << n; if (n % 16 == 0 or n % 10 == 6) cout << "符合標準"; else cout << "不符合標準"; } ``` ### 8-6 拆數字 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; reverse(s.begin(), s.end()); cout << s; } ``` ### 8-7 是否為5的倍數-2? ```cpp= #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; if ((s[0] - '0') % 5) cout << "百位數" << s[0] << "不是5的倍數"; else cout << "百位數" << s[0] << "是5的倍數"; cout << '\n'; if ((s[1] - '0') % 5) cout << "十位數" << s[1] << "不是5的倍數"; else cout << "十位數" << s[1] << "是5的倍數"; cout << '\n'; if ((s[2] - '0') % 5) cout << "個位數" << s[2] << "不是5的倍數"; else cout << "個位數" << s[2] << "是5的倍數"; } ``` ### 8-8 是否為迴文? ```cpp= #include <bits/stdc++.h> using namespace std; int main() { string a, b; cin >> a; b = a; reverse(a.begin(), a.end()); cout << b << ": "; if (a == b) cout << "是迴文"; else cout << "不是迴文"; } ``` ### 8-9 自主學習 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n == 5) cout << "80分"; else if (n == 7) cout << "64分"; else if (n == 18) cout << "72分"; else cout << "成績不存在"; } ``` ## 9. 巢狀選擇 ### 9-1 分數等第 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << "分數" << n << "等第"; if (n >= 90) cout << "A!"; else if (n >= 80) cout << "B!"; else if (n >= 70) cout << "C!"; else if (n >= 60) cout << "D!"; else cout << "F!"; cout << '\n'; } ``` ### 9-2 正三角形嗎? ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; cout << a << ' ' << b << ' ' << c; if ((a + b <= c) or (a + c <= b) or (b + c <= a)) cout << ": 無法構成三角形"; else if (a == b and b == c) cout << ": 正三角形"; else cout << ": 非正三角形"; } ``` ### 9-3 玩玩二分法 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n > 0) { cout << n << "是正數\n"; if (n <= 10000) cout << "小於等於10000: A\n"; else cout << "大於10000: B\n"; } else { cout << n << "是負數\n"; if (n <= -10000) cout << "小於等於-10000: C\n"; else cout << "大於-10000: D\n"; } } ``` ### 9-4 自主學習 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { char c; cin >> c; if (c >= 'a' and c <= 'z') cout << "小寫\n"; else if (c >= 'A' and c<= 'Z') cout << "大寫\n"; else cout << "都不是\n"; } ``` ## 10. 多選一 ### 10-1 分數等第 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n > 100 or n < 0) { cout << "error data\n"; return 0; } cout << "Your score is "<< n << " and degree is "; if (n >= 90) cout << "A!"; else if (n >= 80) cout << "B!"; else if (n >= 70) cout << "C!"; else if (n >= 60) cout << "D!"; else cout << "F!"; cout << '\n'; } ``` ### 10-2 點套餐 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n == 1) cout << "牛奶\n西瓜\n檸檬水\n吐司"; else if (n == 2) cout << "西瓜\n檸檬水\n吐司"; else if (n == 3) cout << "檸檬水\n吐司"; else if (n == 4) cout << "吐司"; else cout << "超出範圍\n"; } ``` ### 10-3 象限 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { float x, y; cin >> x >> y; if (x > 0 and y > 0) cout << "第一象限"; if (x < 0 and y > 0) cout << "第二象限"; if (x < 0 and y < 0) cout << "第三象限"; if (x > 0 and y < 0) cout << "第四象限"; if (!x and y) cout << "y軸"; if (!y and x) cout << "x軸"; if (!x and !y) cout << "原點"; } ``` ### 10-4 年齡說 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << n << "歲是"; if (n <= 14) cout << "小孩子"; else if (n <= 29) cout << "志於學"; else if (n <= 39) cout << "而立之年"; else if (n <= 49) cout << "不惑之年"; else if (n <= 59) cout << "知天命之年"; else if (n <= 69) cout << "耳順之年"; else cout << "從心所欲,不逾矩之年"; } ``` ### 10-5 三角形型別 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; cout << a << ' ' << b << ' ' << c << ": "; if (a == b and b == c) cout << "正三角形"; else if ((a == b and b != c) or (b == c and c != a) or (a == c and c != b)) cout << "等腰三角形"; else if (a + b <= c or b + c <= a or a + c <= b) cout << "無法構成三角形"; else if (a * a + b * b == c * c or b * b + c * c == a * a or a * a + c * c == b * b) cout << "直角三角形"; else cout << "一般三角形"; } ``` ### 10-6 自主學習 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << n << "年"; if ((n % 4 == 0 and n % 100 != 0) or n % 400 == 0) cout << "閏年\n"; else cout << "平年\n"; } ``` ## 11. 多選一: switch(C or C++) ### 11-1 字元 ```cpp= #include<bits/stdc++.h> using namespace std; int main() { char c; cin >> c; cout << c; } ``` ### 11-2 點套餐 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { switch('a'){} char type; cin >> type; if (type == 'A') cout << "牛奶\n西瓜\n檸檬水\n吐司"; else if (type == 'B') cout << "西瓜\n檸檬水\n吐司"; else if (type == 'C') cout << "檸檬水\n吐司"; else cout << "吐司"; } ``` ### 11-3 點食物 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { switch('A'); char type; cin >> type; if (type == 'a' or type == 'A') cout << "牛奶"; else if (type == 'b' or type == 'B') cout << "西瓜"; else if (type == 'c' or type == 'C') cout << "檸檬水"; else cout << "吐司"; } ``` ### 11-4 運算 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { switch('A'); int a, b; char type; cin >> type >> a >> b; if (type == 'a' or type == 'A') cout << a << '+' << b << '=' << a + b; else if (type == 'b' or type == 'B') cout << a << '-' << b << '=' << a - b; else if (type == 'c' or type == 'C') cout << a << '*' << b << '=' << a * b; else if (type == 'd' or type == 'D') cout << a << '/' << b << '=' << a / b; else cout << a << '%' << b << '=' << a % b; } ``` ### 11-5 運算列表 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { switch('A'); char type; int a, b; cin >> type >> a >> b; if (type == 'a' or type == 'A') { cout << a << '+' << b << '=' << a + b << '\n'; cout << a << '-' << b << '=' << a - b << '\n'; cout << a << '*' << b << '=' << a * b << '\n'; cout << a << '/' << b << '=' << a / b << '\n'; cout << a % b; } else if (type == 'b' or type == 'B') { cout << a << '-' << b << '=' << a - b << '\n'; cout << a << '*' << b << '=' << a * b << '\n'; cout << a << '/' << b << '=' << a / b << '\n'; cout << a % b; } else if (type == 'c' or type == 'C') { cout << a << '*' << b << '=' << a * b << '\n'; cout << a << '/' << b << '=' << a / b << '\n'; cout << a % b; } else if (type == 'd' or type == 'D') { cout << a << '/' << b << '=' << a / b << '\n'; cout << a % b; } else cout << a % b; } ``` ### 11-6 自主學習 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { switch('A'); int a, b; char type; cin >> a >> type >> b; if (type == '+') cout << a + b; if (type == '-') cout << a - b; if (type == '*') cout << a * b; if (type == '/') cout << a / b; if (type == '%') cout << a % b; if (type == '^') cout << pow(a, b); if (type == '#') cout << pow(a, 1.0/b); } ``` ## 12. 直覺不結構的if...goto(C or C++) ### 12-1 印10次 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { for (int i = 0 ;i <10;i++) cout << "HaHaHa!\n"; } ``` ### 12-2 印N次 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) cout << "HaHaHa!\n"; } ``` ### 12-3 月份判斷 ```cpp= #include <bits/stdc++.h using namespace std; int main() { int n; while (cin >> n) { if (n < 1) break; if (n > 12) cout << "超出範圍"; else cout << n << "月是"; if (n == 12 or n == 1 or n == 2) cout << "冬天"; if (n >= 3 and n <= 5) cout << "春天"; if (n >= 6 and n <= 8) cout << "夏天"; if (n >= 9 and n <= 11) cout << "秋天"; cout << '\n'; } } ``` ### 12-4 自主學習 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { for (int i = 0;i < 5;i++) { bool flag = 1, f = 1; int a[5]; for (int j = 0;j < 5;j++) cin >> a[j]; if (a[0] > a[1] and a[1] > a[2] and a[2] > a[3] and a[3] > a[4]) cout << "此數列嚴格遞減\n此數列遞減\n"; else if (a[0] >= a[1] and a[1] >= a[2] and a[2] >= a[3] and a[3] >= a[4]) cout << "此數列遞減\n"; else if (a[0] < a[1] and a[1] < a[2] and a[2] < a[3] and a[3] < a[4]) cout << "此數列嚴格遞增\n此數列遞增\n"; else if (a[0] <= a[1] and a[1] <= a[2] and a[2] <= a[3] and a[3] <= a[4]) cout << "此數列遞增\n"; else cout << "End\n"; } } ``` ## 13. 重複-計數器控制 ### 13-1 奇數還是偶數? ```cpp= #include <bits/stdc++.h> using namespace std; int main() { for (int i = 0;i < 10;i++) { int n; cin >> n; if (n % 2) cout << n << "是奇數\n"; else cout << n << "是偶數\n"; } } ``` ### 13-2 印100次 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n = 100; while (n--) cout << "HaHaHa!印我100次\n"; } ``` ### 13-3 印N次 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) cout << "這是重複結構的固定次數範例\n"; } ``` ### 13-4 輸入10個浮點數 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { float x; for (int i = 0;i < 10;i++) { cin >> x; cout << fixed << setprecision(2) << x << '\n'; } } ``` ### 13-5 累加 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int ans = 0; for (int i = 0;i < 10;i++) { int x; cin >> x; ans += x; cout << "第" << i + 1 << "次累加" << ans << '\n'; } cout << "累加和" << ans; } ``` ### 13-6 輸入N個浮點數 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { float a; cin >> a; cout << fixed << setprecision(2) << a << '\n'; } } ``` ### 13-7 計算總和 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { float ans = 0; int n; cin >> n; while (n--) { float x; cin >> x; ans += x; } cout << fixed << setprecision(2) << ans; } ``` ### 13-8 計算平均 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { float ans = 0; int n; cin >> n; int t = n; while (n--) { float x; cin >> x; ans += x; } cout << fixed << setprecision(2) << ans << '\n' << ans / t; } ``` ### 13-9 最高分 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { float ans = 0; int n; cin >> n; while (n--) { float x; cin >> x; ans = max(ans, x); } cout << fixed << setprecision(2) << ans; } ``` ### 13-10 成績計算 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { int a, b; cin >> a >> b; cout << a + b << '\n'; } } ``` ### 13-11 自主學習 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 2;i <= n;i++) { bool flag = 0; for (int j = 2;j < i;j++) if (i % j == 0) flag = 1; if (flag) cout << i << '\n'; } } ``` ## 14. 重複-警示值控制 ### 14-1 奇數還是偶數? ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { if (!n) break; if (n % 2) cout << n << "是奇數\n"; else cout << n << "是偶數\n"; } } ``` ### 14-2 印到我說停 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { if (!n) break; cout << "警示值範例程式!\n"; } } ``` ### 14-3 直到我說停 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { if (n < 0) break; cout << n << "\n"; } } ``` ### 14-4 重複幾次 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n, ans = 0; while (cin >> n) { if (n < 0) break; ans++; } cout << ans << "筆資料"; } ``` ### 14-5 總和 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n, ans = 0; while (cin >> n) { if (n < 0) break; ans += n; } cout << "總和=" << ans; } ``` ### 14-6 班級均分-完成 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n, ans = 0, t = 0; while (cin >> n) { if (n < 0) break; ans += n; t++; } cout << "總和=" << ans << "\n平均="; cout << fixed << setprecision(2) << 1.0 * ans / t; } ``` ### 14-7 最高分 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n, ans = 0; while (cin >> n) { if (n < 0) break; ans = max(ans, n); } cout << ans; } ``` ### 14-8 自主學習 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { if (n <= 1) break; bool flag = 1; for (int i = 2;i < n;i++) { if (n % i == 0) flag = 0; } if (flag) cout << "質數\n"; else cout << "合數\n"; } } ``` ## 15. for ### 15-1 1到100和 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { cout << "1+2+3+...+100=5050"; } ``` ### 15-2 奇數和 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { cout << "1+3+5+...+99=2500"; } ``` ### 15-3 偶數和 ```cpp= #include <bits/stdc++.h> //#include <iostream> using namespace std; int main() { cout << "2+4+6+...+100=2550"; } ``` ### 15-4 1到N之和 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int ans = 0; for (int i = 1;i <= n;i++) ans += i; cout << "1+2+3+...+n=" << ans; } ``` ### 15-5 X到Y之和 ```cpp= #include <bits/stdc++.h> //#include <iostream> using namespace std; int main() { int x, y; cin >> x >> y; int ans = 0; for (int i = x;i <= y;i++) ans += i; cout << "x到y的和=" << ans; } ``` # Python :::info ***我兒子做了C++的解答了 by 淳*** ::: ## 2. 輸出函式 ### 2-1 測試系統 ```python= print('Hello, DICE!') ``` ### 2-2 Hello, World! ```python= print('Hello, World!\nHello,World!') ``` ### 2-3 印三角形 ```python= for i in range(1,7): print('*' * i) for j in range(1, 6)[::-1]: print('*' * j) ``` ### 2-4 不是計算 ```python= print('2/3=5\n2*3=5') ``` ### 2-5 來個表格 ```python= for i in range(1, 11): print(i, i**2, i**3, sep='\t') ``` ## 3. 變數與指定運算子 ### 3-1 指定整數 ```python= print(3) ``` ### 3-2 加上一些形容詞 ```python= print('There are 33 cats.') ``` ### 3-3 指定浮點數 ```python= print(5.200002, 10.100001, sep='\n') ``` ### 3-4 整數與浮點數 ```python= print('My height is 170 cm.\nMy weight is 55.222 kg.') ``` ### 3-5 =不是等號 ```python= print(555, 555) ``` ### 3-6 自主學習 ```python= print('a') ``` ## 4. 輸入函式 ### 4-1 1個整數 ```python= print(input()) ``` ### 4-2 3個整數 ```python= print(*tuple(map(int, input().split()))[::-1]) ``` ### 4-3 浮點數 ```python= a, b = float(input()), float(input()) print(round(a, 6), round(b, 6), sep='\n') ``` ### 4-4 3個浮點數 ```python= a = [float(input()) for _ in range(3)] print(round(a[-1], 3), round(a[1], 2), round(a[0], 1), sep='\n') ``` ### 4-5 長方形 ```python= print(input(), input(), input()) ``` ### 4-6 自主學習 ```python= print(chr(int(input()))) ``` ## 5. 算術運算子 ### 5-1 四則運算 ```python= a, b = int(input()), int(input()) print(f'''{a}+{b}={a+b} {a}-{b}={a-b} {a}*{b}={a*b} {a}/{b}={a//b}''') ``` ### 5-2 餘數 ```python= a, b = int(input()), int(input()) print(f'{a}/{b}={a//b}餘{a%b}\n{a//b + a%b}') ``` ### 5-3 梯形面積 ```python= a, b, c = int(input()), int(input()), int(input()) print(f'梯形面積{(a+b)*c/2:.1f}平方公分') ``` ### 5-4 溫度換算 ```python= print(f'華氏溫度={(9.0/5.0)*float(input()) + 32:.1f}') ``` ### 5-5 三次溫度換算 ```python= a = [int(input()) for _ in range(3)] for i in range(1, 4): print(f'{i}.華氏溫度={(9/5)*a[i-1] + 32:.1f}') ``` ### 5-6 時間計算 ```python= a = int(input()) print(f'{a}個粽子共需要{a*95 // 60}分{a*95 % 60}秒') ``` ### 5-7 自主學習 ```python= print(input().upper()) ``` ## 6. 單一選擇 ### 6-1 及格嗎? ```python= a = int(input()) if a >= 60: print(f'{a}分及格') print('Over!') ``` ### 6-2 正數? ```python= a = int(input()) if a > 0: print(f'{a}是正數') print('Over!') ``` ### 6-3 偶數? ```python= a = int(input()) if a % 2 == 0: print(f'{a}是偶數') print('Over!') ``` ### 6-4 發現不相等 ```python= a, b = int(input()), int(input()) if a != b: print(f'{a}!={b}') print('Over!') ``` ### 6-5 輸出正數 ```python= print(f'{abs(float(input())):.2f}') ``` ### 6-6 自主學習 ```python= ARR = (2300, 800, 500, 1500) a, b = map(int, input().split()) print('符合預算' if ARR[a-1]+ARR[b-1]<=3000 else '超出預算') ``` ## 7. 用if交換變數 ### 7-1 比大小 ```python= print(f'最大值{max(tuple(map(int, input().split())))}') ``` ### 7-2 三數比大小 ```python= a = tuple(map(int, input().split())) print(max(a), min(a)) ``` ### 7-3 五數比大小 ```python= a = tuple(map(int, input().split())) print(max(a), min(a)) ``` ### 7-4 自主學習 ```python= a = int(input()) if a % 4: print('平年') elif a % 100: print('閏年') elif a % 400: print('平年') else: print('閏年') ``` ## 8. 雙重選擇 ### 8-1 及格與否 ```python= a = int(input()) print(f'{a}分{"不" if a < 60 else ""}及格') ``` ### 8-2 奇數還是偶數? ```py= a = int(input()) print(f'{a}: {"偶" if a % 2 == 0 else "奇"}數') ``` ### 8-3 是否能構成三角形? ```python= i = input() a,b,c = map(int, i.split()) print(f'{i}: {"" if a+b>c and a+c>b and b+c>a else "不"}可以構成三角形') ``` ### 8-4 是否直角三角形? ```python= i = input() a,b,c = map(int, i.split()) print(f'{i}: {"" if a**2+b**2==c**2 or a**2+c**2==b**2 or b**2+c**2==a**2 else "不是"}直角三角形') ``` ### 8-5 是否與6相關? ```python= a = input() print(f'{a}{"" if int(a) % 6 == 0 or a[-1]=="6" else "不"}符合標準') ``` ### 8-6 拆數字 ```python= print(input()[::-1]) ``` ### 8-7 是否為5的倍數-2? ```python= db5 = lambda n: f'{n}{"" if int(n)%5==0 else "不"}是5的倍數' a, b = input(), '百十個' for i in range(3): print(f'{b[i]}位數{db5(a[i])}') ``` ### 8-8 是否為迴文? ```python= a = input() print(f'{a}: {"" if a==a[::-1] else "不"}是迴文') ``` ### 8-9 自主學習 ```python= a = input() if a == '5': print('80分') elif a == '7': print('64分') elif a == '18': print('72分') else: print('成績不存在') ``` ## 9. 巢狀選擇 ### 9-1 分數等第 ```python= def level(score): if 90<=score<=100: return 'A' elif 80<=score<=89: return 'B' elif 70<=score<=79: return 'C' elif 60<=score<=69: return 'D' else: return 'F' a = int(input()) print(f'分數{a}等第{level(a)}!') ``` ### 9-2 正三角形嗎? ```python= def tri(a, b, c): if a == b == c: return '正' elif a+b>c and a+c>b and b+c>a: return '非正' return '無法構成' i = input() a, b, c = map(int, i.strip().split()) print(f'{i}: {tri(a,b,c)}三角形') ``` ### 9-3 玩玩二分法 ```python= a = int(input()) if a > 0: print(f'{a}是正數\n{"小於等於10000: A" if a<=10000 else "大於10000: B"}') else: print(f'{a}是負數\n{"小於等於-10000: C" if a<=-10000 else "大於-10000: D"}') ``` ### 9-4 自主學習 ```python= a = input() if 'a' <= a <= 'z': print('小寫') elif 'A' <= a <= 'Z': print('大寫') else: print('都不是') ``` ## 10. 多選一 ### 10-1 分數等第 ```python= def deg(a): if 100 >= a >= 90: return 'A' elif 89 >= a >= 80: return 'B' elif 79 >= a >= 70: return 'C' elif 69 >= a >= 60: return 'D' elif 59 >= a >= 0: return 'F' else: raise ValueError try: a = int(input()) print(f'Your score is {a} and degree is {deg(a)}!') except: print('error data') ``` ### 10-2 點套餐 ```python= table = ('牛奶', '西瓜', '檸檬水', '吐司') a = int(input()) if a == 1: print(*table, sep='\n') elif a == 2: print(*table[1:], sep='\n') elif a == 3: print(*table[2:], sep='\n') elif a == 4: print(table[-1], sep='\n') else: print('超出範圍') ``` ### 10-3 象限 ```python= a, b = float(input()), float(input()) if a == b == 0: print('原點') elif a == 0: print('y軸') elif b == 0: print('x軸') else: if a > 0 and b > 0: c = '一' elif a < 0 and b > 0: c = '二' elif a < 0 and b < 0: c = '三' else: c = '四' print(f'第{c}象限') ``` ### 10-4 年齡說 ```python= age = int(input()) if 1<=age<=14: deg = '小孩子' elif 15<=age<=29: deg = '志於學' elif 30<=age<=39: deg = '而立之年' elif 40<=age<=49: deg = '不惑之年' elif 50<=age<=59: deg = '知天命之年' elif 60<=age<=69: deg = '耳順之年' else: deg = '從心所欲,不逾矩之年' print(f'{age}歲是{deg}') ``` ### 10-5 三角形型別 ```python= i = input() a, b, c = map(int, i.strip().split()) if a == b == c: d = '正' elif a == b or b == c or c == a: d = '等腰' elif a**2+b**2==c**2 or a**2+c**2==b**2 or b**2+c**2==a**2: d = '直角' elif a+c>b and a+b>c and b+c>a: d = '一般' else: d = '無法構成' print(f'{i}: {d}三角形') ``` ### 10-6 自主學習 ```python= a = int(input()) print(f'{a}年', end='') if not a % 400: print('閏年') elif not a % 100: print('平年') elif not a % 4: print('閏年') else: print('平年') ``` ## 11. 多選一: switch(C or C++) ### 11-1 字元 ```python= print(input()) ``` ### 11-2 點套餐 ```python= meal = '牛奶', '西瓜', '檸檬水', '吐司' i = input() if i == 'A': print(*meal, sep='\n') elif i == 'B': print(*meal[1:], sep='\n') elif i == 'C': print(*meal[2:], sep='\n') else: print(meal[-1]) ``` ### 11-3 點食物 ```python= i = input().upper() if i == 'A': print('牛奶') elif i == 'B': print('西瓜') elif i == 'C': print('檸檬水') else: print('吐司') ``` ### 11-4 運算 ```python= i = input().upper() n1, n2 = int(input()), int(input()) if i == 'A': print(f'{n1}+{n2}={n1+n2}') elif i == 'B': print(f'{n1}-{n2}={n1-n2}') elif i == 'C': print(f'{n1}*{n2}={n1*n2}') elif i == 'D': print(f'{n1}/{n2}={n1//n2}') else: print(f'{n1}%{n2}={n1%n2}') ``` ### 11-5 運算列表 ```python= add = lambda x,y: f'{x}+{y}={x+y}' minus = lambda x,y: f'{x}-{y}={x-y}' times = lambda x,y: f'{x}*{y}={x*y}' dev = lambda x,y: f'{x}/{y}={x//y}' mod = lambda x,y: x%y inp = input().upper() a,b = int(input()), int(input()) if inp == 'A': print(add(a,b), minus(a,b), times(a,b), dev(a,b), mod(a,b), sep='\n') elif inp == 'B': print(minus(a,b), times(a,b), dev(a,b), mod(a,b), sep='\n') elif inp == 'C': print(times(a,b), dev(a,b), mod(a,b), sep='\n') elif inp == 'D': print(dev(a,b), mod(a,b), sep='\n') else: print(mod(a,b)) ``` ### 11-6 自主學習 ```python= inp = input().replace('/','//').replace('^', '**') if '#' not in inp: print(eval(inp)) else: a, b = map(int, inp.split(' # ')) result = a ** (1/b) print(round(result)) ``` ## 12. 直覺不結構的if...goto(C or C++) ### 12-1 印10次 ```python= for _ in range(10): print('HaHaHa!') ``` ### 12-2 印N次 ```python= for _ in range(int(input())): print('HaHaHa!') ``` ### 12-3 月份判斷 ```python= while True: i = int(input()) if i < 0: break if i in (12,1,2): print(f'{i}月是冬天') elif i in (3,4,5): print(f'{i}月是春天') elif i in (6,7,8): print(f'{i}月是夏天') elif i in (9,10,11): print(f'{i}月是秋天') else: print('超出範圍') ``` ### 12-4 自主學習 ```python= while True: try: a = list(map(int, input().split())) if a[0]>a[1]>a[2]>a[3]>a[4]: print('此數列嚴格遞減') if a[0]>=a[1]>=a[2]>=a[3]>=a[4]: print('此數列遞減') else: print('End') except: break ``` ## 13. 重複-計數器控制 ### 13-1 奇數還是偶數? ```python= for _ in range(10): a = int(input()) print(f'{a}是{"奇" if a%2 else "偶"}數') ``` ### 13-2 印100次 ```python= for _ in range(100): print('HaHaHa!印我100次') ``` ### 13-3 印N次 ```python= for _ in range(int(input())): print('這是重複結構的固定次數範例') ``` ### 13-4 輸入10個浮點數 ```python= for _ in range(10): print(f'{float(input()):.2f}') ``` ### 13-5 累加 ```python= a = 0 for i in range(1, 11): a += int(input()) print(f'第{i}次累加{a}') print(f'累加和{a}') ``` ### 13-6 輸入N個浮點數 ```python= for _ in range(int(input())): print(f'{float(input()):.2f}') ``` ### 13-7 計算總和 ```python= t = 0 for _ in range(int(input())): t += float(input()) print(f'{t:.2f}') ``` ### 13-8 計算平均 ```python= time, total = int(input()), 0 for _ in range(time): total += float(input()) print(f'{total:.2f}\n{total/time:.2f}') ``` ### 13-9 最高分 ```python= m = 0 for _ in range(int(input())): m = max(m, float(input())) print(f'{m:.2f}') ``` ### 13-10 成績計算 ```python= for _ in range(int(input())): print(sum(map(int, input().split()))) ``` ### 13-11 自主學習 ```python= def is_prime(n): if n < 2: return False for i in range(2, int(n**0.5)+1): if n % i == 0: return False return True for i in range(2, int(input())+1): if not is_prime(i): print(i) ``` ## 14. 重複-警示值控制 ### 14-1 奇數還是偶數? ```python= while True: a = int(input()) if a == 0: break print(f'{a}是{"奇" if a%2 else "偶"}數') ``` ### 14-2 印到我說停 ```python= while True: if input() == '0': break print('警示值範例程式!') ``` ### 14-3 直到我說停 ```python= while True: a = int(input()) if a < 0: break print(a) ``` ### 14-4 重複幾次 ```python= t = 0 while True: if int(input()) < 0: break t += 1 print(f'{t}筆資料') ``` ### 14-5 班級均分-總和 ```python= s = 0 while True: a = int(input()) if a < 0: break s += a print(f'總和={s}') ``` ### 14-6 班級均分-完成 ```python= t, s = 0, 0 while True: a = int(input()) if a < 0: break t += 1 s += a print(f'總和={s}\n平均={s/t:.2f}') ``` ### 14-7 最高分 ```python= m = 0 while True: a = int(input()) if a < 0: break elif m < a: m = a print(m) ``` ### 14-8 自主學習 ```python= def prime(n): if n < 2: return False for i in range(2, n): if n % i == 0: return False return True while True: a = int(input()) if a == 1: break print('質數' if prime(a) else '合數') ``` ## 15. for ### 15-1 1到100和 ```python= print('1+2+3+...+100=5050') ``` ### 15-2 奇數和 ```python= print('1+3+5+...+99=2500') ``` ### 15-3 偶數和 ```python= print('2+4+6+...+100=2550') ``` ### 15-4 1到N之和 ```python= n = sum(range(1, int(input())+1)) print(f'1+2+3+...+n={n}') ``` ### 15-5 X到Y之和 ```python= a, b = map(int, input().split()) print(f'x到y的和={sum(range(a, b+1))}') ``` ### 15-6 遞減值 ```python= for i in range(1000, 0, -int(input())): print(i, end=' ') ``` ### 15-7 公比 ```python= r, lim = int(input()), int(input()) arr, now = [1], 1 while now <= lim: now *= r arr.append(now) print('公比數列: ', *arr[:-1]) ``` ### 15-8 等比級數公式 ```python= a1, an, r = map(int, input().split()) now, s = a1, 0 while now <= an: s += now now *= r print(f'首項: {a1}\n末項: {an}\n公比: {r}\n公比數列: {s}') ``` ### 15-9 2的次方數 ```python= n = int(input()) i = 0 while True: if 2 ** i > n: print(f'第一個大於{n}的數是{2 ** i}') break i += 1 ``` ### 15-10 自主學習 ```python= def f(n): if n==1: return 10 elif n==2: return 15 elif n==3: return 16 return f(n-2)+f(n-3) print(f(int(input()))) ``` ## 16. 重複結構與判斷式 ### 16-1 及格與不及格人數 ```python= p, g = 0, 0 for _ in range(10): a = int(input()) if a >= 60: p += 1 if a > 80: g += 1 print(f'及格{p}人\n不及格{10-p}人\n超過80分{g}人') ``` ### 16-2 最大公因數 ```python= from math import gcd a, b, c = map(int, input().split()) g = [i for i in range(1, min(a,b,c)+1) if a%i==0 and b%i==0 and c%i==0] print(f'{a} {b} {c}的公因數', end='') print(*g) print(f'{a} {b} {c}的最大公因數{gcd(a, gcd(b, c))}') ``` ### 16-3 if...break ```python= t = 0 for i in range(2, int(input())+1, 2): if i % 10 == 0: break print(i) t += i print(t) ``` ### 16-4 質數判斷 ```python= def is_prime(n): if n < 2: return False for i in range(2, n): if n % i == 0: return False return True n = int(input()) print(f'{n}: {"" if is_prime(n) else "不是"}質數') ``` ### 16-5 互質 ```python= from math import gcd n = input() a, b = map(int, n.split()) print(f'{n}: 兩數{"" if gcd(a, b) == 1 else "不"}互質') ``` ### 16-6 continue ```python= t = 0 for i in range(2, int(input())+1, 2): if i % 10 == 0: continue print(i) t += i print(t) ``` ### 16-7 5的倍數不加 ```python= t = 0 for i in range(1, int(input())+1): if i % 5 == 0: continue print(i) t += i print(t) ``` ### 16-8 相反數絕對值-EOF ```python= from sys import stdin for i in stdin: i = int(i) print(-i, abs(i)) ``` ### 16-9 象限-EOF ```python= from sys import stdin for i in stdin: x, y = map(float, i.split()) if x == y == 0: print('原點') elif x == 0: print('y軸') elif y == 0: print('x軸') else: if x>0 and y>0: print('第一象限') elif x<0 and y<0: print('第三象限') elif x>0 and y<0: print('第四象限') else: print('第二象限') ``` ### 16-10 分數等第人數 ```python= lv = [0] * 5 s = 'A', 'B', 'C', 'D', 'F' while True: try: score = input().strip().upper() if score == 'A': lv[0] += 1 elif score == 'B': lv[1] += 1 elif score == 'C': lv[2] += 1 elif score == 'D': lv[3] += 1 elif score == 'F': lv[4] += 1 except: for i in range(5): print(f'{s[i]} {lv[i]}人') break ``` ### 16-11 自主學習 ```python= print(len(input())) ``` ## 17. 巢狀迴圈 ### 17-1 九九乘法 ```python= for i in range(1, 10): for j in range(1, 10): print(f'{i}*{j}={i*j}') ``` ### 17-2 九九乘法表 ```python= for i in range(1, 10): for j in range(1, 10): print(f'{i}*{j}={i*j:2d}', end='\t') print() ``` ### 17-3 1到N乘法表 ```python= for i in range(1, int(input())+1): for j in range(1, 10): print(f'{i}*{j}={i*j}') ``` ### 17-4 N到9乘法表 ```python= for i in range(int(input()), 10): for j in range(1, 10): print(f'{i}*{j}={i*j}') ``` ### 17-5 乘法變形排列 ```python= def f(s, e): for i in range(1, 10): for j in range(s, e + 1): print(f'{j}*{i}={j * i}', end='\t') print() f(1, 3) f(4, 6) f(7, 9) ``` ### 17-6 重複質數判斷 ```python= def prime(n): if n < 2: return False for i in range(2, n): if n % i == 0: return False return True while True: a = int(input()) if a == 1: break print('質數' if prime(a) else '合數') ``` ### 17-7 找因數 ```python= while True: a = int(input()) if not a: break for i in range(1, a+1): if a % i == 0: print(i, end=' ') print() ``` ### 17-8 階乘值-1 ```python= from math import factorial as f for i in range(1, int(input())+1): print(f'{i}!={f(i)}') ``` ### 17-9 階乘值-2 ```python= from math import factorial as f while True: a = int(input()) if not a: break print(f'{a}!={f(a)}') ``` ### 17-10 印圖形1 ```python= for i in range(1, int(input())+1): print(str(i) * i) ``` ### 17-11 印圖形2 ```python= def f(n): for i in range(1, n+1): print(i, end='') print() for i in range(1, int(input())+1): f(i) ``` ## 18. 字元字串與ASCII ### 18-1 字元 ```python= print('a', input(), sep='\n') ``` ### 18-2 字元變十進位 ```python= print(ord(input())) ``` ### 18-3 十進位變字元 ```python= print(chr(int(input()))) ``` ### 18-4 大寫變小寫 ```python= print(input().lower()) ``` ### 18-5 小寫變大寫 ```python= print(input().upper()) ``` ### 18-6 字串 ```python= print(input()) ``` ### 18-7 小寫字串 ```python= print(*input().upper()[:3]) ``` ### 18-8 大寫字串 ```python= print(*input().lower()[:3]) ``` ### 18-9 解碼 ```python= print(*((chr(ord(i)-2)) for i in input()), sep='') ```