# C++ 基礎語法練習題:Unit-3 比較運算、邏輯運算 、if 判斷、switch 判斷 講義不是我寫的,原文連結為 [Yui Huang 演算法學習筆記:C++ 基礎語法](https://yuihuang.com/syntax/) 我只是將自己寫的練習題程式碼記錄下來。 最後更新日期:2024年11月9日 ## [ZeroJudge: a003. 兩光法師占卜術](https://zerojudge.tw/ShowProblem?problemid=a003) ### Python 程式碼 執行時間最久約為 19 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= M, D = map(int, input().split()) S = (M*2 + D) % 3 if S == 0: print("普通") elif S == 1: print("吉") else: print("大吉") ``` ### C 程式碼 使用 if ... else,執行時間最久約為 2 ms,使用記憶體最多約為 88 kB,通過測試。 ```c= #include <stdio.h> int main() { int M, D, S; scanf("%d %d", &M, &D); S = (M*2 + D) % 3; if (S == 0) { printf("普通\n"); } else if (S == 1) { printf("吉\n"); } else { printf("大吉\n"); } return 0; } ``` 使用 switch,執行時間最久約為 2 ms,使用記憶體最多約為 88 kB,通過測試。 ```c= #include <stdio.h> int main() { int M, D, S; scanf("%d %d", &M, &D); S = (M*2 + D) % 3; switch(S) { case 0: printf("普通\n"); break; case 1: printf("吉\n"); break; default: printf("大吉\n"); } return 0; } ``` ### C++ 程式碼 使用 if ... else,執行時間最久約為 2 ms,使用記憶體最多約為 324 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int M, D, S; cin >> M >> D; S = (M*2 + D) % 3; if (S == 0) { cout << "普通\n"; } else if (S == 1) { cout << "吉\n"; } else { cout << "大吉\n"; } return 0; } ``` 使用 switch,執行時間最久約為 2 ms,使用記憶體最多約為 324 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int M, D, S; cin >> M >> D; S = (M*2 + D) % 3; switch(S) { case 0: cout << "普通\n"; break; case 1: cout << "吉\n"; break; default: cout << "大吉\n"; } return 0; } ``` ## [ZeroJudge: d058. BASIC 的 SGN 函數](https://zerojudge.tw/ShowProblem?problemid=d058) ### Python 程式碼 使用 if ... else,執行時間最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= n = int(input()) if n > 0: print(1) elif n < 0: print(-1) else: print(0) ``` 不使用 if,執行時間最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= n = int(input()) print((n > 0) - (n < 0)) ``` ### C++ 程式碼 使用 if ... else,執行時間最久約為 2 ms,使用記憶體最多約為 324 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; if (n > 0) { cout << "1\n"; } else if (n < 0) { cout << "-1\n"; } else { cout << "0\n"; } return 0; } ``` 不使用 if,執行時間最久約為 2 ms,使用記憶體最多約為 324 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; cout << (n > 0) - (n < 0) << "\n"; return 0; } ``` ## [ZeroJudge: d060. 還要等多久啊?](https://zerojudge.tw/ShowProblem?problemid=d060) ### Python 程式碼 使用 if ... else,執行時間最久約為 17 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= m = int(input()) if 0 <= m <= 25: print(25-m) else: print(60 - m + 25) ``` 不使用 if,執行時間最久約為 21 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= print((25 - int(input()) + 60) % 60) ``` ### C++ 程式碼 使用 if ... else,執行時間最久約為 2 ms,使用記憶體最多約為 320 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int m; cin >> m; if (m >= 0 && m <= 25) { cout << 25 - m << "\n"; } else { cout << 60 - m + 25 << "\n"; } return 0; } ``` 不使用 if,執行時間最久約為 2 ms,使用記憶體最多約為 336 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int m; cin >> m; cout << (25 - m + 60) % 60 << "\n"; return 0; } ``` ## [ZeroJudge: d063. 0 與 1](https://zerojudge.tw/ShowProblem?problemid=d063) ### Python 程式碼 使用 if ... esle,執行時間最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= n = int(input()) if n == 0: print(1) else: print(0) ``` 使用 XOR,執行時間最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= print(f"{1^(int(input())):d}") ``` 使用 bool 及 not,執行時間最久約為 20 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= print(int(not(int(input())))) ``` ### C++ 程式碼 使用 if ... else,執行時間最久約為 2 ms,使用記憶體最多約為 316 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; if (n == 0) cout << "1\n"; else cout << "0\n"; return 0; } ``` 使用一行的 if,執行時間最久約為 2 ms,使用記憶體最多約為 312 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; cout << (n == 0 ? 1 : 0) << "\n"; return 0; } ``` 使用 XOR,執行時間最久約為 2 ms,使用記憶體最多約為 320 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; cout << (1^n) << "\n"; return 0; } ``` 使用 !,執行時間最久約為 2 ms,使用記憶體最多約為 308 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; cout << !n << "\n"; return 0; } ``` ## [ZeroJudge: d064. ㄑㄧˊ 數?](https://zerojudge.tw/ShowProblem?problemid=d064) ### Python 程式碼 執行時間最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= print("Odd" if int(input())%2 else "Even") ``` 改用位元運算,如果 1 與偶數取 and 結果為 0,反之則為 1。執行時間最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= print("Odd" if int(input())&1 else "Even") ``` ### C++ 程式碼 執行時間最久約為 3 ms,使用記憶體最多約為 328 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; cout << (n%2 ? "Odd" : "Even") << "\n"; return 0; } ``` 改用位元運算,如果 1 與偶數取 and 結果為 0,反之則為 1。執行時間最久約為 2 ms,使用記憶體最多約為 324 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; cout << (n&1 ? "Odd" : "Even") << "\n"; return 0; } ``` ## [ZeroJudge: d065. 三人行必有我師 (1 行版)](https://zerojudge.tw/ShowProblem?problemid=d065) ### Python 程式碼 執行時間最久約為 19 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= print(max(map(int, input().split()))) ``` ### C++ 程式碼 使用 if ... else,執行時間最久約為 2 ms,使用記憶體最多約為 316 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int a, b, c, imax; cin >> a >> b >> c; if (a > b) imax = a; else imax = b; if (c > imax) imax = c; cout << imax << "\n"; return 0; } ``` 使用兩層 max,執行時間最久約為 2 ms,使用記憶體最多約為 328 kB,通過測試。 ```cpp= #include <iostream> #include <algorithm> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; cout << max(a, max(b, c)) << "\n"; return 0; } ``` 由大到小排序,執行時間最久約為 2 ms,使用記憶體最多約為 324 kB,通過測試。 ```cpp= #include <iostream> #include <algorithm> using namespace std; int main() { int a[3]; for(int i=0; i<3; i++) cin >> a[i]; sort(a, a+3, greater<>()); cout << a[0] << "\n"; return 0; } ``` ## [ZeroJudge: d067. 格瑞哥里的煩惱 (1 行版)](https://zerojudge.tw/ShowProblem?problemid=d067) ### Python 程式碼 執行時間最久約為 19 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= y = int(input()) if y%400 == 0: print("a leap year") elif y%100 == 0: print("a normal year") elif y%4 == 0: print("a leap year") else: print("a normal year") ``` ### C++ 程式碼 執行時間最久約為 2 ms,使用記憶體最多約為 340 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int y; cin >> y; if (y%400 == 0) cout << "a leap year\n"; else if (y%100 == 0) cout << "a normal year\n"; else if (y%4 == 0) cout << "a leap year\n"; else cout << "a normal year\n"; return 0; } ``` ## [ZeroJudge: d068. 該減肥了!](https://zerojudge.tw/ShowProblem?problemid=d068) ### Python 程式碼 使用 if,執行時間最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= w = int(input()) if w > 50: print(w-1) else: print(w) ``` 不使用 if,執行時間最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= w = int(input()) print(w - (w>50)) ``` ### C++ 程式碼 使用 if,執行時間最久約為 2 ms,使用記憶體最多約為 324 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int w; cin >> w; if (w > 50) cout << w-1 << "\n"; else cout << w << "\n"; return 0; } ``` 不使用 if,執行時間最久約為 2 ms,使用記憶體最多約為 320 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int w; cin >> w; cout << w - (w > 50) << "\n"; return 0; } ``` ## [ZeroJudge: e835. p2.表演座位 (Seats)](https://zerojudge.tw/ShowProblem?problemid=e835) ### Python 程式碼 執行時間最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= n = int(input()) b, r, c = 0, 0, 0 if 1 <= n <= 2500: b = 1 r = (n-1)//25 + 1 c = n - (r-1)*25 elif 2501 <= n <= 7500: b = 2 m = n - 2500 r = (m-1)//50 + 1 c = m - (r-1)*50 else: b = 3 m = n - 7500 r = (m-1)//25 + 1 c = m - (r-1)*25 print(f"{b:d} {r:d} {c:d}") ``` ### C++ 程式碼 執行時間最久約為 2 ms,使用記憶體最多約為 336 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int n, m, b, r, c; cin >> n; if (n >= 1 && n <= 2500) { b = 1; r = (n-1)/25 + 1; c = n - (r-1)*25; } else if (n >= 2501 && n <= 7500) { b = 2; m = n - 2500; r = (m-1)/50 + 1; c = m - (r-1)*50; } else { b = 3; m = n - 7500; r = (m-1)/25 + 1; c = m - (r-1)*25; } cout << b << " " << r << " " << c << "\n"; return 0; } ``` ## [ZeroJudge: d066. 上學去吧!](https://zerojudge.tw/ShowProblem?problemid=d066) ### Python 程式碼 全部換成分鐘會比較簡單,執行時間最久約為 19 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= h, m = map(int, input().split()) t = h*60 + m print("At School" if 7*60 + 30 <= t < 17*60 else "Off School") ``` ### C++ 程式碼 執行時間最久約為 3 ms,使用記憶體最多約為 320 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int h, m; cin >> h >> m; int t = h*60 + m; cout << (t >= 7*60 + 30 && t < 17*60 ? "At School" : "Off School") << "\n"; return 0; } ``` ## [ZeroJudge: d669. 11677 - Alarm Clock](https://zerojudge.tw/ShowProblem?problemid=d669) ### Python 程式碼 執行時間最久約為 17 ms,使用記憶體最多約為 3.3 MB,通過測試。 ```python= while True: h1, m1, h2, m2 = map(int, input().split()) if h1 == 0 and m1 == 0 and h2 == 0 and m2 == 0: break t1 = h1*60 + m1 t2 = h2*60 + m2 print((t2 - t1 + 1440) % 1440) ``` ### C++ 程式碼 執行時間最久約為 2 ms,使用記憶體最多約為 316 kB,通過測試。 ```cpp= #include <iostream> using namespace std; int main() { int h1, m1, h2, m2; while(true) { cin >> h1 >> m1 >> h2 >> m2; if (h1 == 0 && m1 == 0 && h2 == 0 && m2 == 0) break; int t1 = h1*60 + m1, t2 = h2*60 + m2; cout << (t2 - t1 + 1440) % 1440 << "\n"; } return 0; } ``` ------ ###### tags:`演算法`、`APCS`、`Python`、`C++`