---
title: Python DOMjudge 教材
tags: Python, DOMjudge
description: 線上評分系統, ICPC, Program, Online
---
## Python DOMjudge 教材
### 輸入處理
#### 單個輸入(練習TA001)
```python=
1
```
```python=
import sys
print(sys.stdin.read())
```
#### 單行Input
```=
22 3
```
```python=
M,D = map(int,input().split()) #一行輸入
```
#### 多行輸入輸出 範例
##### 範例輸入資料
```=
22 1
23 1
24 1
```
##### 範例輸出資料
```=
23
24
25
```
##### Python 程式碼
:::danger
Windows 上 Python會有些功能無法與其他OS產生相同結果
請勿使用字串檔案路徑,請使用例如`os`等內建函式庫
:::
`main.py`
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = [int(num) for num in line.split()]
# nums = map(int, line.split())
print(sum(nums))
```
本地端測試指令(bash/CMD 上輸入,`$`不需要輸入)
1. `$ cd /pc/file/path/your/python/file/target/folder`
2. `$ python main.py < in.txt`
請上傳 `main.py` 至評分系統,程式語言選 **py3/python3**
### 兩數相加(一行輸入)
:::info
輸入兩整數,輸出兩數之和。
:::
#### Input
```=
22 3
```
#### Output
```=
25
```
#### Answer
```python=
a, b=map(int,input().split())
print(a+b)
```
```python=
num1, num2 = map(int, input().split())
sum = num1+num2
print(sum)
```
https://marketplace.visualstudio.com/items?itemName=dongli.python-preview
<iframe width="800" height="350" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=num1,%20num2%20%3D%20map%28int,%20input%28%29.split%28%29%29%0Asum%20%3D%20num1%2Bnum2%0Aprint%28sum%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=1&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%2222%203%22%5D&textReferences=false"></iframe>
### 長方形面積
:::info
輸入長length和寬width,輸出其面積Area,長方形面積Area = length * width。
:::
#### Input
```=
22 3
```
#### Output
```=
66
```
#### Answer
```python=
l, w = map(int, input().split())
print(l*w)
```
```python=
x,y=map(int,input().split())
a=int(x*y)
print(a)
```
### G001:長寬高算體積
:::info
輸入長寬高,輸出物體之體積。(請使用int)
:::
#### Input
```=
2 3 4
```
#### Output
```=
24
```
#### Answer
```python=
x,y,z=map(int,input().split())
a=int(x*y*z)
print(a)
```
<iframe width="800" height="320" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=x,%20y%20%3D%20map%28int,%20input%28%29.split%28%29%29%0Aa%20%3D%20int%28x%20*%20y%29%0Aprint%28a%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%2222%203%22%5D&textReferences=false"> </iframe>
### hello, world
:::info
輸入字串 "world", 則輸出 "hello, world"
:::
#### Input
```=
world
```
```=
C#
```
#### Output
```=
hello, world
```
```=
hello, C#
```
#### Answer
```python=
line = input()
print("hello, "+line)
```
---
#### 多行Input
```=
22 1
23 2
24 3
```
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = []
for num in line.split():
nums.append(int(num))
```
#### Input取字元
```=
567
345
```
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
chars = []
for num in line.split():
for char in num:
chars.append(int(char))
```
### 數字、字串與變數
Python內建的資料類型有列幾種:
+ 布林:True 或 False (又稱為真或假)
+ 整數:65535
+ 浮點數:65535.000,即有小數點的數字。
+ 字串:'65535',一串字元,前後可用單引號「'」或雙引號「"」包住。
```python=
>>> type(True)
<class 'bool'>
>>> type(65535)
<class 'int'>
```
在Python中如何定義一個變數與其資料類型,例如:x 是一個變數名稱,再透過「=」指派一個值給變數x
```python=
>>> x = 65535
>>> x
65535
>>> type(x)
<class 'int'>
```
多重指派
在Python中可以將多個值指派給多個變數,值跟變數的數量要相同。
```python=
>>> x, y = 2, 3
>>> x
2
>>> y
3
```
```python=
>>> x, y = 2, 3
>>> x, y = y, x
>>> x
3
>>> y
2
```
資料類型轉換
在Python中可以做用內建函數,例如:int()、float()、str()等,來對不同的資料類型可以做轉換。
```python=
>>> x = 1.0
>>> int(x)
1
>>> x = 1.5
>>> int(x)
1
```
變數的命名規則
在Python中要定義一個變數名稱只能使用下列這些字元,變數名稱也不能是Python的保留字:
小寫字母 (a~z)
大寫字母 (A~Z)
數字 (0~9)
底線 (_)
```python=
>>> _x = 1
>>> _x
1
```
```python=
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue',
'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global',
'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise',
'return', 'try', 'while', 'with', 'yield']
```
### 二數相加(多行輸入)
:::info
二數相加
:::
#### Input (in.txt)
```=
22 4
23 5
24 3
```
#### Output
```=
26
28
27
```
#### Answer 1
`main.py`
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = []
for num in line.split():
nums.append(int(num))
# nums = map(int, line.split())
print(sum(nums))
```
`$ python main.py < in.txt`
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=fake_input%20%3D%20%22%22%22%0A22%204%0A23%205%0A24%203%0A%22%22%22.strip%28%29.encode%28%29%0A%0Afor%20line%20in%20fake_input.splitlines%28%29%3A%0A%20%20%20%20%23%20splitlines%20%E6%9C%83%E5%8E%BB%E9%99%A4%E4%B8%8D%E5%90%8COS%E7%9A%84%E6%8F%9B%E8%A1%8C%E7%AC%A6%E8%99%9F%0A%20%20%20%20nums%20%3D%20%5B%5D%0A%20%20%20%20for%20num%20in%20line.split%28%29%3A%0A%20%20%20%20%20%20%20%20nums.append%28int%28num%29%29%0A%20%20%20%20%23%20nums%20%3D%20map%28int,%20line.split%28%29%29%0A%20%20%20%20print%28sum%28nums%29%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=26&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=true"> </iframe>
#### Answer 2
`main.py`
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = [int(num) for num in line.split()]
# nums = map(int, line.split())
print(sum(nums))
```
`$ python main.py < in.txt`
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = []
for num in line.split():
nums.append(int(num))
print(nums[0]+nums[1])
```
#### Answer 3
```python=
#! /usr/bin/python3
import sys
for line in sys.stdin.read().splitlines():
ab = line.split()
a = int(ab[0])
b = int(ab[1])
print(a+b)
```
### 二數相減(多行輸入,自行練習)
:::info
二數相減
:::
---
### 公斤磅數
:::info
由使用者輸入公斤數,請轉為磅數再輸出。
註: (1) 1磅=0.454公斤(1磅=〜0.45359237公斤)(大約)。
(2) 輸入值可能有小數,不會是負數。
(3) 輸出值小數留2位,四捨五入。
提示:num取小數2位, 四捨五入
num = round(num, 2)
:::
#### Input
```=
10
23
10.2
23.88
23.87
```
#### Output
```=
22.03
50.66
22.47
52.6
52.58
```
#### Answer
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = []
for num in line.split():
nums.append(float(num))
print(round(nums[0]/0.454, 2))
```
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=fake_input%20%3D%20%22%22%22%0A10%0A23%0A10.2%0A23.88%0A23.87%0A%22%22%22.strip%28%29.encode%28%29%0A%0Afor%20line%20in%20fake_input.splitlines%28%29%3A%0A%20%20%20%20%23%20splitlines%20%E6%9C%83%E5%8E%BB%E9%99%A4%E4%B8%8D%E5%90%8COS%E7%9A%84%E6%8F%9B%E8%A1%8C%E7%AC%A6%E8%99%9F%0A%20%20%20%20nums%20%3D%20%5B%5D%0A%20%20%20%20for%20num%20in%20line.split%28%29%3A%0A%20%20%20%20%20%20%20%20nums.append%28float%28num%29%29%0A%20%20%20%20print%28round%28nums%5B0%5D/0.454,%202%29%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe>
### 磅數公斤(練習)
:::info
由使用者輸入磅數,請轉為公斤數再輸出。
註: (1) 1磅=0.454公斤(1磅=〜0.45359237公斤)(大約)。
(2) 輸入值可能有小數,不會是負數。
(3) 輸出值小數留2位,四捨五入。
提示:num取小數2位, 四捨五入
num = round(num, 2)
:::
:::success
A00
https://zerojudge.ntub.tw/public/problems/75
:::
#### Input
```=
10
23
10.2
23.88
23.87
100
1000
10000
100000
```
#### Output
```=
4.54
10.44
4.63
10.84
10.84
45.4
454.0
4540.0
45400.0
```
<!--
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = []
for num in line.split():
nums.append(float(num))
print(round(float(nums[0]) * 0.454,2))
```
-->
### 攝氏轉華氏
:::info
溫度單位
攝氏 °C (Celsius):目前大多數的國家使用的。
華氏 °F (Fahrenheit):僅剩美國在使用。
輸入攝氏溫度,輸出華氏溫度。(華式F)=(攝氏C)*9/5+32,小數部分請無條件捨去。
F = C * 9 / 5 + 32 或 F= C * 1.8 +32
:::
#### Input
```=
10
23
36
-20
66
```
#### Output
```=
F=50
F=73
F=96
F=-4
F=150
```
#### Answer
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = []
for num in line.split():
nums.append(float(num))
F = int((nums[0]*9/5)+32)
print("F=" + str(F))
```
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = []
for num in line.split():
nums.append(float(num))
F = int((nums[0]*9/5)+32)
print("F={}".format(F))
```
---
### 華氏轉攝氏(練習)
:::info
溫度單位
華氏 °F (Fahrenheit):僅剩美國在使用。
攝氏 °C (Celsius):目前大多數的國家使用的。
華氏與攝氏溫度的關係是:
C= (F – 32) * 5 / 9
輸入華氏溫度,輸出攝氏溫度。小數部分請無條件捨去。
:::
:::success
A01
https://zerojudge.ntub.tw/jury/problems/76
:::
#### Input
```=
60
100
32
-20
0
```
#### Output
```=
C=15
C=37
C=0
C=-28
C=-17
```
<!--
#### Answer
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = []
for num in line.split():
nums.append(float(num))
C = int((nums[0] - 32) * 5 / 9)
print("C=" + str(C))
```
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = []
for num in line.split():
nums.append(float(num))
C = int((nums[0] - 32) * 5 / 9)
print("C={}".format(C))
```
---
-->
### BMI(練習)
:::info
身高體重指數(英語:Body Mass Index,簡稱BMI),又稱身體質量指數,是由一個人的質量(體重)和身高計算出的一個數值。BMI的定義是體重除以身高的平方,以公斤/平方公尺為單位表示,由體重(公斤)和身高(公尺)得出:
$\mbox{BMI}=\frac{w}{h^{2}}$
其中$w$為體重(weight);$h$為身高(height)。
:::
:::success
A02
https://zerojudge.ntub.tw/jury/problems/77
:::
提示:
```python=
# 計算BMI
bmi = weight / ((height/100)**2)
# BMI取小數2位, 四捨五入
bmi = round(bmi, 2)
```
#### Input
輸入包含有二個整數weight height。height:代表體重,單位公斤; height:代表身高,單位公分。
```=
72 178
34 160
45 160
100 100
61 101
```
#### Output
BMI取小數2位, 四捨五入
```=
22.72
13.28
17.58
100.0
59.8
```
<!--
#### Answer
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = []
for num in line.split():
nums.append(int(num))
weight = nums[0]
height = nums[1]
bmi = weight / ((height/100)**2)
# BMI取小數2位, 四捨五入
bmi = round(bmi, 2)
print(bmi)
```
-->
---
### 次方求餘
:::info
輸入三個正整數 n p d,輸出(n^p)%d,即 n 的 p次方 對 d的餘數。
:::
#### Input
```=
2 4 5
```
#### Output
```=
1
```
#### Answer
```python=
n,p,d=map(int,input().split())
ans=(n**p)%d
print(ans)
```
### 整數商餘 (作業)
:::info
輸入兩整數 m、n,輸出 m 除以 n 之 商 及 餘數。
在Python有兩種除法:
1. 使用/運算得出商的結果是浮點數。 7/2 = 3.5
2. 使用//運算得出商的結果是整數。7//2 = 3
3. 用%來得到整除後的餘數,求餘數。 7%2 =1 , 7%4=3
:::
:::success
A03
https://zerojudge.ntub.tw/jury/problems/78
:::
#### Input
```=
7 2
7 4
8 2
```
#### Output
```=
3 1
1 3
4 0
```
#### Answer
```c=
#include <stdio.h>
int main()
{
int m,n;
scanf("%d%d",&m,&n);
printf("\n%d / %d=%d\n%d mod %d=%d",m,n,m/n,m,n,m%n);
}
```
### 四數有權重的相加(作業)
:::info
輸入:四個數字$a \: b \: c \: d$ 有權重的相加 輸出: $56*a +24*b +14*c +6*d$.
:::
:::success
A04
https://zerojudge.ntub.tw/jury/problems/79
:::
#### Input
```=
1 1 1 1
1 10 100 1000
```
#### Output
```=
100
7696
```
<!--
#### Answer 1
main.py`
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = []
for num in line.split():
nums.append(int(num))
print(nums[0]*56+nums[1]*24+nums[2]*14+nums[3]*6)
```
-->
### 分組
:::info
要同學分組,分組的方式為依座號順序,每 3 個人一組。如:1, 2, 3 為第一組,4, 5, 6 為第二組….以此類推。輸入同學的座號,請判斷他在哪一組。
:::
#### Input
輸入只有一行,含有一個正整數 n,代表同學的座號。
```=
7
```
```=
12
```
#### Output
輸出該同學的組別。
```=
3
```
```=
4
```
<!--
#### Answer
【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d073
【解題想法】除法取整
```cpp=
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
cout << ((n - 1) / 3) + 1 << "\n";
}
```
-->
```python=
n = int(input())
print((n - 1) // 3 + 1)
```
---
### 錢(作業)
:::info
周杰倫A,蔡依林B和JJ,三個人都有錢。
+ 1.周杰倫A 和 蔡依林B 加起來有$X$元。 $A+B=X$
+ 2.蔡依林B 和 JJ 加起來有$Y$元。 $B+JJ=Y$
+ 3.周杰倫A 和 JJ 加起來有$Z$元。 $A+JJ=Z$
每個人有多少元?
輸入:三個正偶數$X \: Y \: Z$數字 輸出: 周杰倫A 蔡依林B JJ ,每個人各多少元.
<!-- http://140.114.86.238/problem/11099/ -->
:::
:::success
A05
https://zerojudge.ntub.tw/jury/problems/80
:::
#### Input
```=
170 160 190
168 158 188
```
#### Output
```=
100 70 90
99 69 89
```
<!--
#### Answer 1
```python=
import sys
for line in sys.stdin.read().splitlines():
# splitlines 會去除不同OS的換行符號
nums = []
for num in line.split():
nums.append(int(num))
x = nums[0]
y = nums[1]
z = nums[2]
print(int((x+z-y)/2), int((x+y-z)/2), int((y+z-x)/2))
```
-->
---
---
### 閏年 leap year,讓同學了解不同輸入格式的處理方式
:::info
除了不是400的倍數的100的倍數以外,四的倍數的年份均為閏年,閏年這年會多一天 (2月29日)。
任何能以 4 整除的年份都是閏年:例如 1988 年、1992 年及 1996 年都是閏年。
不過,仍必須將一個小錯誤列入考量。西曆規定能以 100 (例如1900 年) 整除的年份,同時也要能以 400 整除,才算是閏年。
下列年份不是閏年:1700、1800、1900、2100、2200、2300、2500、2600。
原因是這些年份能以 100 整除,但無法以 400 整除。
下列年份為閏年:1600、2000、2400。
:::
#### Input
輸入只有一行,其中含有一個正整數 y,代表西元年份。
```=
1992
```
```=
2100
```
```=
2021
```
```=
2000
```
#### Output
若 y 是閏年,請輸出「a leap year」,否則請輸出「a normal year」
```=
a leap year
```
```=
a normal year
```
```=
a normal year
```
```=
a leap year
```
#### Answer
ZeroJudge d067: 格瑞哥里的煩惱 (1 行版)
【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d067
```python=
y = int(input())
if ((y%4 == 0) and (y%100 != 0)) or (y%400 == 0):
print("a leap year")
else:
print("a normal year")
```
ZeroJudge d069: 格瑞哥里的煩惱 (t 行版)
【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d069
#### Input
```=
4
1992
1993
1900
2000
```
#### Output
```=
a leap year
a normal year
a normal year
a leap year
```
```python=
t = int(input())
for _ in range(t):
y = int(input())
if ((y%4==0) and (y%100!=0)) or (y%400==0):
print("a leap year")
else:
print("a normal year")
```
```python=
import sys
lines = sys.stdin.readlines()
t = int(lines[0])
for i in range(1, t+1):
y = int(lines[i])
if ((y%4==0) and (y%100!=0)) or (y%400==0):
print("a leap year")
else:
print("a normal year")
```
ZeroJudge d072: 格瑞哥里的煩惱 (Case 版)
【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d072
#### Input
```=
4
1992
1993
1900
2000
```
#### Output
```=
Case 1: a leap year
Case 2: a normal year
Case 3: a normal year
Case 4: a leap year
```
```python=
num = int(input())
for TCount in range(1, num+1):
print(f"Case {TCount}: ", end="")
y = int(input())
if ((y%4==0) and (y%100!=0)) or (y%400==0):
print("a leap year")
else:
print("a normal year")
```
ZeroJudge d070: 格瑞哥里的煩惱 (0 尾版)
【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d070
#### Input
```=
1992
1993
1900
2000
0
```
#### Output
```=
a leap year
a normal year
a normal year
a leap year
```
```python=
while True:
y = int(input())
if y == 0:
break
if ((y%4==0) and (y%100!=0)) or (y%400==0):
print("a leap year")
else:
print("a normal year")
```
ZeroJudge d071: 格瑞哥里的煩惱 (EOF 版)
【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d071
#### Input
```=
1992
1993
1900
2000
```
#### Output
```=
a leap year
a normal year
a normal year
a leap year
```
```python=
while True:
try:
y = int(input())
if ((y%4==0) and (y%100!=0)) or (y%400==0):
print("a leap year")
else:
print("a normal year")
except:
break
```
```python=
import sys
for line in sys.stdin.readlines():
y = int(line)
if ((y%4==0) and (y%100!=0)) or (y%400==0):
print("a leap year")
else:
print("a normal year")
```
### 輸出星期幾(練習)
:::info
給你 2021 年的某月某日,輸出星期幾。
:::
:::success
A99
https://zerojudge.ntub.tw/jury/problems/81
:::
#### Input
第一行有個T代表詢問的日期數(T<=100)
接下來會有T行,每行都有一組M、D分別代表某月某日
```=
9
1 6
2 28
4 5
5 26
8 1
11 1
12 25
12 31
3 9
```
#### Output
輸出那天星期幾
```=
Wednesday
Sunday
Monday
Wednesday
Sunday
Monday
Saturday
Friday
Tuesday
```
<!--
```=
Thursday
Monday
Tuesday
Thursday
Monday
Tuesday
Sunday
Saturday
Wednesday
```
-->
---
<!-- -->
### 出題模板
:::info
說明 $x^2$
:::
#### Input
```=
1 2 1 3 4
```
#### Output
```=
2
```
#### Answer
```python=
#Code
```
---