# C++ 基礎語法練習題:Unit-2 基本輸入與輸出、變數、算數運算、資料型態
講義不是我寫的,原文連結為 [Yui Huang 演算法學習筆記:C++ 基礎語法](https://yuihuang.com/syntax/)
我只是將自己寫的練習題程式碼記錄下來。
最後更新日期:2024年11月6日
## [ZeroJudge: a001. 哈囉](https://zerojudge.tw/ShowProblem?problemid=a001)
### Python 程式碼
執行時間最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。
```python=
print(f"hello, {input():s}")
```
### C++ 程式碼
執行時間最久約為 2 ms,使用記憶體最多約為 316 kB,通過測試。
```cpp=
#include <iostream>
#include <string>
using namespace std;
int main() {
string s; cin >> s;
cout << "hello, " << s << "\n";
return 0;
}
```
## [ZeroJudge: a002. 簡易加法](https://zerojudge.tw/ShowProblem?problemid=a002)
### Python 程式碼
執行時間最久約為 20 ms,使用記憶體最多約為 3.3 MB,通過測試。
```python=
a, b = map(int, input().split())
print(a+b)
```
一行解,執行時間最久約為 21 ms,使用記憶體最多約為 3.3 MB,通過測試。
```python=
print(sum(map(int, input().split())))
```
### C++ 程式碼
執行時間最久約為 3 ms,使用記憶體最多約為 324 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int a, b; cin >> a >> b;
cout << a+b << "\n";
return 0;
}
```
也可以使用 scnaf 及 printf,執行時間最久約為 2 ms,使用記憶體最多約為 100 kB,通過測試。
```cpp=
#include <cstdio>
using namespace std;
int main() {
int a, b; scanf("%d %d", &a, &b);
printf("%d\n", a+b);
return 0;
}
```
## [ZeroJudge: d049. 中華民國萬歲!](https://zerojudge.tw/ShowProblem?problemid=d049)
### Python 程式碼
執行時間最久約為 19 ms,使用記憶體最多約為 3.3 MB,通過測試。
```python=
print(int(input()) - 1911)
```
### C++ 程式碼
執行時間最久約為 3 ms,使用記憶體最多約為 336 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int year; cin >> year;
cout << year - 1911 << "\n";
return 0;
}
```
也可以使用 scnaf 及 printf,執行時間最久約為 2 ms,使用記憶體最多約為 92 kB,通過測試。
```cpp=
#include <cstdio>
using namespace std;
int main() {
int year; scanf("%d", &year);
printf("%d\n", year - 1911);
return 0;
}
```
## [ZeroJudge: d050. 妳那裡現在幾點了?](https://zerojudge.tw/ShowProblem?problemid=d050)
### Python 程式碼
執行時間最久約為 19 ms,使用記憶體最多約為 3.3 MB,通過測試。
```python=
print((int(input()) - 15 + 24) % 24)
```
### C++ 程式碼
執行時間最久約為 2 ms,使用記憶體最多約為 328 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int h; cin >> h;
cout << (h - 15 + 24) % 24 << "\n";
return 0;
}
```
也可以使用 scnaf 及 printf,執行時間最久約為 2 ms,使用記憶體最多約為 76 kB,通過測試。
```cpp=
#include <cstdio>
using namespace std;
int main() {
int h; scanf("%d", &h);
printf("%d\n", (h - 15 + 24) % 24);
return 0;
}
```
## [ZeroJudge: d051. 糟糕,我發燒了!](https://zerojudge.tw/ShowProblem?problemid=d051)
### Python 程式碼
執行時間最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。
```python=
print(f"{(int(input()) - 32) * 5 / 9:.3f}")
```
### C++ 程式碼
整數格式要用 long,換算後的浮點數格式要用 double,固定輸出小數點格式需要引入函式庫 iomanip。執行時間最久約為 2 ms,使用記憶體最多約為 340 kB,通過測試。
```cpp=
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
long T; cin >> T;
cout << fixed << setprecision(3) << double((T - 32) * 5) / 9.0 << "\n";
return 0;
}
```
整數格式要用 long,換算後的浮點數格式要用 double,用 printf 可以很方便地控制輸出小數點位數。執行時間最久約為 2 ms,使用記憶體最多約為 92 kB,通過測試。
```cpp=
#include <cstdio>
using namespace std;
int main() {
long T; scanf("%ld", &T);
printf("%.3f\n", double((T - 32) * 5) / 9.0);
return 0;
}
```
## [ZeroJudge: d073. 分組報告](https://zerojudge.tw/ShowProblem?problemid=d073)
### Python 程式碼
執行時間最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。
```python=
print(f"{(int(input()) - 1) // 3 + 1:d}")
```
### C++ 程式碼
執行時間最久約為 3 ms,使用記憶體最多約為 336 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int n; cin >> n;
cout << (n-1)/3 + 1 << "\n";
return 0;
}
```
執行時間最久約為 5 ms,使用記憶體最多約為 80 kB,通過測試。
```cpp=
#include <cstdio>
using namespace std;
int main() {
int n; scanf("%d", &n);
printf("%d\n", (n-1)/3 + 1);
return 0;
}
```
## [ZeroJudge: d827. 買鉛筆](https://zerojudge.tw/ShowProblem?problemid=d827)
### Python 程式碼
執行時間最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。
```python=
n = int(input())
print(f"{n//12*50 + n%12*5:d}")
```
### C++ 程式碼
執行時間最久約為 2 ms,使用記憶體最多約為 336 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int n; cin >> n;
cout << n/12*50 + n%12*5 << "\n";
return 0;
}
```
執行時間最久約為 2 ms,使用記憶體最多約為 84 kB,通過測試。
```cpp=
#include <cstdio>
using namespace std;
int main() {
int n; scanf("%d", &n);
printf("%d\n", n/12*50 + n%12*5);
return 0;
}
```
------
###### tags:`演算法`、`APCS`、`Python`、`C++`