# 0-4 條件判斷與基礎邏輯
講義不是我寫的,網址在此 https://emanlaicepsa.github.io/2020/10/21/0-index/
我只是將自己寫的練習題程式碼記錄下來。
最後更新日期:2024年10月5日
## [TOJ: 94 / 判斷季節](https://toj.tfcis.org/oj/pro/94/)
### Python 程式碼
寫法1,用 if else 判斷季節。執行時間最久約為 26 ms,使用記憶體最多約為 4232 kB,通過測試。
```python=
month = int(input())
if 3 <= month <= 5: print("Spring!")
elif 6 <= month <= 8: print("Summer!")
elif 9 <= month <= 11: print("Autumn!")
else: print("Winter!")
```
寫法2,將用份對應的季節存入 tuple,由於索引值由0開始,開頭放入空字串,讀取輸入的月份轉成整數,印出對應的季節。寫法2看起來有點笨,但其實在某些狀況下很好用。執行時間最久約為 26 ms,使用記憶體最多約為 4116 kB,通過測試。
```python=
season = ("", "Winter!", "Winter!", "Spring!",
"Spring!", "Spring!", "Summer!",
"Summer!", "Summer!", "Autumn!",
"Autumn!", "Autumn!", "Winter!")
print(season[int(input())])
```
### C++ 程式碼
寫法1,用 if else 判斷季節。執行時間最久約為 1 ms,使用記憶體最多約為 360 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int month; cin >> month;
if (month >= 3 && month <= 5) cout << "Spring!\n";
else if (month >= 6 && month <= 8) cout << "Summer!\n";
else if (month >= 9 && month <= 11) cout << "Autumn!\n";
else cout << "Winter!\n";
return 0;
}
```
寫法2,將用份對應的季節存入陣列。執行時間最久約為 1 ms,使用記憶體最多約為 284 kB,通過測試。
```cpp=
#include <iostream>
#include <string>
using namespace std;
int main() {
string season[13] = {"", "Winter!", "Winter!", "Spring!",
"Spring!", "Spring!", "Summer!",
"Summer!", "Summer!", "Autumn!",
"Autumn!", "Autumn!", "Winter!"};
int month; cin >> month;
cout << season[month] << "\n";
return 0;
}
```
## [TOJ: 95 / 體育期末考](https://toj.tfcis.org/oj/pro/95/)
### Python 程式碼
寫法1,使用多層的 if ... else,執行時間最久約為 39 ms,使用記憶體最多約為 4088 kB,通過測試。
```python=
a, b = map(int, input().split())
if a == 1:
if b >= 8: print("PASS!")
else: print("FAIL! You see see you!")
elif a == 2:
if b >= 9: print("PASS!")
else: print("FAIL! You see see you!")
else:
if b >= 10: print("PASS!")
else: print("FAIL! You see see you!")
```
也可以改成這樣,執行時間最久約為 19 ms,使用記憶體最多約為 4088 kB,通過測試。
```python=
a, b = map(int, input().split())
if a == 1 and b >= 8: print("PASS!")
elif a == 2 and b >= 9: print("PASS!")
elif a == 3 and b >= 10: print("PASS!")
else: print("FAIL! You see see you!")
```
改用 tuple 儲存各年級對應的及格標準,執行時間最久約為 25 ms,使用記憶體最多約為 4372 kB,通過測試。
```python=
s = (0, 8, 9, 10)
a, b = map(int, input().split())
if b >= s[a]: print("PASS!")
else: print("FAIL! You see see you!")
```
### C++ 程式碼
寫法1,使用多層的 if ... else,執行時間最久約為 1 ms,使用記憶體最多約為 128 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int a, b; cin >> a >> b;
if (a == 1) {
if (b >= 8) cout << "PASS!\n";
else cout << "FAIL! You see see you!\n";
} else if (a == 2) {
if (b >= 9) cout << "PASS!\n";
else cout << "FAIL! You see see you!\n";
} else {
if (b >= 10) cout << "PASS!\n";
else cout << "FAIL! You see see you!\n";
}
return 0;
}
```
也可以改成這樣,執行時間最久約為 1 ms,使用記憶體最多約為 128 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int a, b; cin >> a >> b;
if (a == 1 &&b >= 8) cout << "PASS!\n";
else if (a == 2 && b >= 9) cout << "PASS!\n";
else if (a == 3 && b >= 10) cout << "PASS!\n";
else cout << "FAIL! You see see you!\n";
return 0;
}
```
改用陣列儲存各年級對應的及格標準,執行時間最久約為 1 ms,使用記憶體最多約為 532 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int s[4] = {0, 8, 9, 10};
int a, b; cin >> a >> b;
if (b >= s[a]) cout << "PASS!\n";
else cout << "FAIL! You see see you!\n";
return 0;
}
```
## [TOJ: 96 / a - Arithmetic](https://toj.tfcis.org/oj/pro/96/)
### Python 程式碼
執行時間最久約為 21 ms,使用記憶體最多約為 4028 kB,通過測試。
```python=
line = input().split()
a = int(line[0])
b = int(line[2])
op = line[1]
if op == '+':
print(f"{a:d} + {b:d} = {a+b:d}")
elif op == '-':
print(f"{a:d} - {b:d} = {a-b:d}")
elif op == '*':
print(f"{a:d} * {b:d} = {a*b:d}")
elif op == '/':
if b == 0: print("ERROR")
else:
print(f"{a:d} / {b:d} = {a//b:d} ... {a%b:d}")
```
### C++ 程式碼
寫法1,使用 if else。執行時間最久約為 1 ms,使用記憶體最多約為 340 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int a, b; char op;
cin >> a >> op >> b;
if (op == '+')
cout << a << " + " << b << " = " << a+b << "\n";
else if (op == '-')
cout << a << " - " << b << " = " << a-b << "\n";
else if (op == '*')
cout << a << " * " << b << " = " << a*b << "\n";
else if (op == '/') {
if (b == 0) cout << "ERROR\n";
else cout << a << " / " << b << " = " << a/b << " ... " << a%b << "\n";
}
return 0;
}
```
寫法2,使用 case。執行時間最久約為 1 ms,使用記憶體最多約為 284 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int a, b; char op;
cin >> a >> op >> b;
switch(op) {
case '+':
cout << a << " + " << b << " = " << a+b << "\n";
break;
case '-':
cout << a << " - " << b << " = " << a-b << "\n";
break;
case '*':
cout << a << " * " << b << " = " << a*b << "\n";
break;
case '/': // 因為題目限制操作只有 +-*/ 等4種,這行可以改成 default:
if (b == 0) cout << "ERROR\n";
else cout << a << " / " << b << " = " << a/b << " ... " << a%b << "\n";
//break; // 最後一項可以不加 break
}
return 0;
}
```
## [TOJ: 102 / f - Floating Point](https://toj.tfcis.org/oj/pro/102/)
### Python 程式碼
執行時間最久約為 129 ms,使用記憶體最多約為 4208 kB,通過測試。
```python=
line = input().split()
a = float(line[0])
b = float(line[2])
op = line[1]
if op == '+':
print(f"{a:.4f} + {b:.4f} = {a+b:.4f}")
elif op == '-':
print(f"{a:.4f} - {b:.4f} = {a-b:.4f}")
elif op == '*':
print(f"{a:.4f} * {b:.4f} = {a*b:.4f}")
elif op == '/':
if b == 0: print("ERROR")
else:
print(f"{a:.4f} / {b:.4f} = {a/b:.4f}")
```
### C++ 程式碼
寫法1,用 iomanip 函式庫的 setprecision 及 fixed 設定並固定輸出的小數位數。注意,**浮點數要用 double 才能過關**,用 float 無法通過其中兩筆測資。執行時間最久約為 4 ms,使用記憶體最多約為 532 kB,通過測試。
```cpp=
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double a, b; char op;
cin >> a >> op >> b;
cout << fixed << setprecision(4);
switch(op) {
case '+':
cout << a << " + " << b << " = " << a+b << "\n";
break;
case '-':
cout << a << " - " << b << " = " << a-b << "\n";
break;
case '*':
cout << a << " * " << b << " = " << a*b << "\n";
break;
case '/':
if (b == 0) {
cout << "ERROR\n";
return 0;
} else cout << a << " / " << b << " = " << a/b << "\n";
break;
}
return 0;
}
```
寫法2,改用 stdio.h 函式庫的 scanf 讀取資料,用 printf 輸出資料。注意,**浮點數要用 double 才能過關**,用 float 無法通過其中兩筆測資。執行時間最久約為 2 ms,使用記憶體最多約為 316 kB,通過測試。
```cpp=
#include <stdio.h>
using namespace std;
int main() {
double a, b; char op;
scanf("%lf %c %lf", &a, &op, &b);
switch(op) {
case '+':
printf("%.4f + %.4f = %.4f\n", a, b, a+b);
break;
case '-':
printf("%.4f - %.4f = %.4f\n", a, b, a-b);
break;
case '*':
printf("%.4f * %.4f = %.4f\n", a, b, a*b);
break;
case '/':
if (b == 0) printf("ERROR\n");
else printf("%.4f / %.4f = %.4f\n", a, b, a/b);
break;
}
return 0;
}
```
## [TOJ: 103 / Is it good to drink](https://toj.tfcis.org/oj/pro/103/)
### Python 程式碼
執行時間最久約為 33 ms,使用記憶體最多約為 4120 kB,通過測試。
```python=
line1 = input().split()
n1 = line1[0]
s1 = int(line1[1])
line2 = input().split()
n2 = line2[0]
s2 = int(line2[1])
if n1 == n2 and s1 == s2: print("GOOD")
elif n1 == n2 or s1 == s2: print("=~=")
else: print("OTZ")
```
### C++ 程式碼
執行時間最久約為 1 ms,使用記憶體最多約為 304 kB,通過測試。
```cpp=
#include <iostream>
#include <string>
using namespace std;
int main() {
string n1, s1, n2, s2;
cin >> n1 >> s1 >> n2 >> s2;
if (n1 == n2 && s1 == s2) cout << "GOOD\n";
else if (n1 == n2 || s1 == s2) cout << "=~=\n";
else cout << "OTZ\n";
return 0;
}
```
## [TOJ: 535 / 3-9.電玩技能綜合檢定](https://toj.tfcis.org/oj/pro/535/)
### Python 程式碼
執行時間最久約為 47 ms,使用記憶體最多約為 4384 kB,通過測試。
```python=
s = int(input())
if s == 100: print("S")
elif 90 <= s <= 99: print("A")
elif 80 <= s <= 89: print("B")
elif 70 <= s <= 79: print("C")
elif 60 <= s <= 69: print("D")
else: print("F")
```
### C++ 程式碼
執行時間最久約為 3 ms,使用記憶體最多約為 532 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int s; cin >> s;
if (s == 100) cout << "S\n";
else if (s >= 90 && s <= 99) cout << "A\n";
else if (s >= 80 && s <= 89) cout << "B\n";
else if (s >= 70 && s <= 79) cout << "C\n";
else if (s >= 60 && s <= 69) cout << "D\n";
else cout << "F\n";
return 0;
}
```
## [TOJ: 538 / A. 二階](https://toj.tfcis.org/oj/pro/538/)
### Python 程式碼
執行時間最久約為 86 ms,使用記憶體最多約為 4348 kB,通過測試。
```python=
english, math, nature = map(int, input().split())
data, program = map(float, input().split())
threshold = float(input())
score = 20.0*(english*1.25 + math*2.0 + nature*1.0)/(15.0*4.25)\
+ data*0.4 + program*0.4
if score >= threshold: print("YA")
else: print("QQ")
```
### C++ 程式碼
執行時間最久約為 6 ms,使用記憶體最多約為 616 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int english, math, nature;
cin >> english >> math >> nature;
float data, program, threshold, score;
cin >> data >> program >> threshold;
score = 20.0*((float)english*1.25 + (float)math*2.0 + (float)nature*1.0)/(15.0*4.25)
+ data*0.4 + program*0.4;
cout << (score >= threshold ? "YA" : "QQ") << "\n";
//if (score >= threshold) cout << "YA\n";
//else cout << "QQ\n";
return 0;
}
```
## [TOJ: 461 / A. 海綿寶寶的時鐘](https://toj.tfcis.org/oj/pro/461/)
### Python 程式碼
寫法1,真的計算時針、分針夾角,執行時間最久約為 31 ms,使用記憶體最多約為 4372 kB,通過測試。
```python=
x = float(input())
if x > 180.0: x = 360.0 - x
x *= 10.0
ans = 0
dh, dm = 0, 0
for h in range(12):
for m in range(60):
diff = abs(dm - dh)
if diff > 1800: diff = 3600 - diff
if diff == int(x): ans += 1
dh += 5
dm += 60
if dh >= 3600: dh -= 3600
if dm >= 3600: dm -= 3600
ans *= 2
print(ans)
```
寫法2,因為時針、分針夾角為 $0^{\circ}$ 只有兩種可能性,就是 00:00 及 12:00;如果夾角為 $180^{\circ}$ 只有兩種可能性,就是 06:00 及 18:00;其它夾角在一天中都會出現 4 次,如果知道這個性質程式碼會簡化很多。執行時間最久約為 19 ms,使用記憶體最多約為 3960 kB,通過測試。
```python=
x = float(input())
if x == 0 or x == 180: print(2)
else: print(4)
```
### C++ 程式碼
寫法1,真的計算時針、分針夾角,執行時間最久約為 3 ms,使用記憶體最多約為 536 kB,通過測試。
```cpp=
#include<iostream>
#include<cmath>
using namespace std;
int main() {
float x; cin >> x;
if (x > 180.0) x = 360.0 - x;
x *= 10.0;
int ans = 0, dh = 0, dm = 0;
for(int h=0; h<12; h++) {
for(int m=0; m<60; m++) {
int diff = abs(dm - dh);
if (diff > 1800) diff = 3600 - diff;
if (diff == int(x)) ans++;
dh += 5;
dm += 60;
if (dh >= 3600) dh -= 3600;
if (dm >= 3600) dm -= 3600;
}
}
ans *= 2;
cout << ans << "\n";
return 0;
}
```
寫法2,因為時針、分針夾角為 $0^{\circ}$ 只有兩種可能性,就是 00:00 及 12:00;如果夾角為 $180^{\circ}$ 只有兩種可能性,就是 06:00 及 18:00;其它夾角在一天中都會出現 4 次,如果知道這個性質程式碼會簡化很多。執行時間最久約為 ms,使用記憶體最多約為 128 kB,通過測試。
```cpp=
#include<iostream>
using namespace std;
int main() {
float x; cin >> x;
if (x == 0 || x == 180) cout << 2 << "\n";
else cout << 4 << "\n";
return 0;
}
```
## [ZeroJudge: f312. 1. 人力分配](https://zerojudge.tw/ShowProblem?problemid=f312)
{%hackmd @yizhewang/ryV42h5A3 %}
------
###### tags:`演算法`、`APCS`、`Python`、`C++`