# 0-0 Hello World! ~ 0-3 四則運算
講義不是我寫的,網址在此 https://emanlaicepsa.github.io/2020/10/21/0-index/
我只是將自己寫的練習題程式碼記錄下來。
最後更新日期:2024年10月5日
## [ZeroJudge: d483. hello, world](https://zerojudge.tw/ShowProblem?problemid=d483)
### Python 程式碼
執行時間最久約為 18 ms,使用記憶體最多約為 3.2 MB,通過測試。
```python=
print("hello, world")
```
### C++ 程式碼
執行時間最久約為 2 ms,使用記憶體最多約為 312 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
cout << "hello, world\n";
return 0;
}
```
## [TOJ: 92 / 天線寶寶說你好](https://toj.tfcis.org/oj/pro/92/)
### Python 程式碼
執行時間最久約為 25 ms,使用記憶體最多約為 4116 kB,通過測試。
```python=
n1, n2, n3 = input().split()
print(f"Hello, {n1:s}, {n2:s}, and {n3:s}!")
#print("Hello, {}, {}, and {}!".format(n1, n2, n3))
```
### C++ 程式碼
執行時間最久約為 1 ms,使用記憶體最多約為 308 kB,通過測試。
```cpp=
#include <iostream>
#include <string>
using namespace std;
int main() {
string n1, n2, n3;
cin >> n1 >> n2 >> n3;
cout << "Hello, " << n1 << ", " << n2 << ", and " << n3 << "!\n";
return 0;
}
```
## [TOJ: 519 / 1-1.山彦](https://toj.tfcis.org/oj/pro/519/)
### Python 程式碼
執行時間最久約為 21 ms,使用記憶體最多約為 4116 kB,通過測試。
```python=
n1, n2 = map(int, input().split())
print(f"Do you want to say {n1:d} and {n2:d} ??")
#print("Do you want to say {:d} and {:d} ??".format(n1, n2))
```
### C++ 程式碼
執行時間最久約為 1 ms,使用記憶體最多約為 308 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int n1, n2;
cin >> n1 >> n2;
cout << "Do you want to say " << n1 << " and " << n2 << " ??\n";
return 0;
}
```
## [TOJ: 520 / 1-2.誰當2P](https://toj.tfcis.org/oj/pro/520/)
### Python 程式碼
執行時間最久約為 28 ms,使用記憶體最多約為 4116 kB,通過測試。
```python=
n1, n2 = map(int, input().split())
print(f"{n2:d} {n1:d}")
#print("{:d} {:d}".format(n2, n1))
```
### C++ 程式碼
執行時間最久約為 1 ms,使用記憶體最多約為 284 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int n1, n2;
cin >> n1 >> n2;
cout << n2 << " " << n1 << "\n";
return 0;
}
```
## [TOJ: 93 / 梯形面積](https://toj.tfcis.org/oj/pro/93/)
### Python 程式碼
執行時間最久約為 28 ms,使用記憶體最多約為 3896 kB,通過測試。
```python=
topline, baseline, height = map(int, input().split())
area = (topline + baseline) * height // 2;
print(area)
```
### C++ 程式碼
執行時間最久約為 1 ms,使用記憶體最多約為 284 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int topline, baseline, height, area;
cin >> topline >> baseline >> height;
area = (topline + baseline) * height / 2;
cout << area << "\n";
return 0;
}
```
## [TOJ: 98 / c - Speed of Light](https://toj.tfcis.org/oj/pro/98/)
### Python 程式碼
執行時間最久約為 50 ms,使用記憶體最多約為 4120 kB,通過測試。
```python=
c = 299792458
print("1 Light-second(LS) is {:d} metres.".format(c))
print("1 Light-minute(LM) is {:d} metres.".format(c*60))
print("1 Light-hour(LH) is {:d} metres.".format(c*60*60))
print("1 Light-day(LD) is {:d} metres.".format(c*60*60*24))
print("1 Light-week(LW) is {:d} metres.".format(c*60*60*24*7))
print("1 Light-year(LY) is {:d} metres.".format(c*60*60*24*365))
```
也可以用比較新的 print 格式。
```python=
c = 299792458
print(f"1 Light-second(LS) is {c:d} metres.")
print(f"1 Light-minute(LM) is {c*60:d} metres.")
print(f"1 Light-hour(LH) is {c*60*60:d} metres.")
print(f"1 Light-day(LD) is {c*60*60*24:d} metres.")
print(f"1 Light-week(LW) is {c*60*60*24*7:d} metres.")
print(f"1 Light-year(LY) is {c*60*60*24*365:d} metres.")
```
因為以上的程式碼中有不少部分是重複的,也可以用變數將之前算過的值存起來,可以少乘幾次。理論上可以用 c \*= 將 c 的值逐次變大,但這樣可能會弄錯目前 c 的值,建議初學者不要這樣寫。
```python=
c = 299792458
LM = c*60
LH = LM*60
LD = LH*24
LW = LD*7
LY = LD*365
print(f"1 Light-second(LS) is {c:d} metres.")
print(f"1 Light-minute(LM) is {LM:d} metres.")
print(f"1 Light-hour(LH) is {LH:d} metres.")
print(f"1 Light-day(LD) is {LD:d} metres.")
print(f"1 Light-week(LW) is {LW:d} metres.")
print(f"1 Light-year(LY) is {LY:d} metres.")
```
### C++ 程式碼
執行時間最久約為 1 ms,使用記憶體最多約為 284 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
long c = 299792458;
long LS = c, LM, LH, LD, LW, LY;
LM = LS*60;
LH = LM*60;
LD = LH*24;
LW = LD*7;
LY = LD*365;
cout << "1 Light-second(LS) is " << LS << " metres.\n";
cout << "1 Light-minute(LM) is " << LM << " metres.\n";
cout << "1 Light-hour(LH) is " << LH << " metres.\n";
cout << "1 Light-day(LD) is " << LD << " metres.\n";
cout << "1 Light-week(LW) is " << LW << " metres.\n";
cout << "1 Light-year(LY) is " << LY << " metres.\n";
return 0;
}
```
## [TOJ: 522 / 2-2.魔力增幅](https://toj.tfcis.org/oj/pro/522/)
### Python 程式碼
執行時間最久約為 20 ms,使用記憶體最多約為 4088 kB,通過測試。
```python=
n = int(input())
print(n*n%10)
```
### C++ 程式碼
執行時間最久約為 2 ms,使用記憶體最多約為 128 kB,通過測試。
```cpp=
#include<iostream>
using namespace std;
int main() {
long n; cin >> n;
cout << n*n%10 << endl;
return 0;
}
```
------
###### tags:`演算法`、`APCS`、`Python`、`C++`