Python DOMjudge 教材

輸入處理

單個輸入(練習TA001)

1
import sys print(sys.stdin.read())

單行Input

22 3
M,D = map(int,input().split()) #一行輸入

多行輸入輸出 範例

範例輸入資料
22 1 23 1 24 1
範例輸出資料
23 24 25
Python 程式碼

Windows 上 Python會有些功能無法與其他OS產生相同結果

請勿使用字串檔案路徑,請使用例如os等內建函式庫

main.py

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

兩數相加(一行輸入)

輸入兩整數,輸出兩數之和。

Input

22 3

Output

25

Answer

a, b=map(int,input().split()) print(a+b)
num1, num2 = map(int, input().split()) sum = num1+num2 print(sum)

https://marketplace.visualstudio.com/items?itemName=dongli.python-preview

長方形面積

輸入長length和寬width,輸出其面積Area,長方形面積Area = length * width。

Input

22 3

Output

66

Answer

l, w = map(int, input().split()) print(l*w)
x,y=map(int,input().split()) a=int(x*y) print(a)

G001:長寬高算體積

輸入長寬高,輸出物體之體積。(請使用int)

Input

2 3 4

Output

24

Answer

x,y,z=map(int,input().split()) a=int(x*y*z) print(a)

hello, world

輸入字串 "world", 則輸出 "hello, world"

Input

world
C#

Output

hello, world
hello, C#

Answer

line = input() print("hello, "+line)

多行Input

22 1 23 2 24 3
import sys for line in sys.stdin.read().splitlines(): # splitlines 會去除不同OS的換行符號 nums = [] for num in line.split(): nums.append(int(num))

Input取字元

567 345
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',一串字元,前後可用單引號「'」或雙引號「"」包住。
>>> type(True) <class 'bool'> >>> type(65535) <class 'int'>

在Python中如何定義一個變數與其資料類型,例如:x 是一個變數名稱,再透過「=」指派一個值給變數x

>>> x = 65535 >>> x 65535 >>> type(x) <class 'int'>

多重指派
在Python中可以將多個值指派給多個變數,值跟變數的數量要相同。

>>> x, y = 2, 3 >>> x 2 >>> y 3
>>> x, y = 2, 3 >>> x, y = y, x >>> x 3 >>> y 2

資料類型轉換
在Python中可以做用內建函數,例如:int()、float()、str()等,來對不同的資料類型可以做轉換。

>>> x = 1.0 >>> int(x) 1 >>> x = 1.5 >>> int(x) 1

變數的命名規則
在Python中要定義一個變數名稱只能使用下列這些字元,變數名稱也不能是Python的保留字:

小寫字母 (a~z)
大寫字母 (A~Z)
數字 (0~9)
底線 (_)

>>> _x = 1 >>> _x 1
>>> 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']

二數相加(多行輸入)

二數相加

Input (in.txt)

22 4 23 5 24 3

Output

26 28 27

Answer 1

main.py

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

Answer 2

main.py

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

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

#! /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)

二數相減(多行輸入,自行練習)

二數相減


公斤磅數

由使用者輸入公斤數,請轉為磅數再輸出。

註: (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

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))

磅數公斤(練習)

由使用者輸入磅數,請轉為公斤數再輸出。

註: (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 100 1000 10000 100000

Output

4.54 10.44 4.63 10.84 10.84 45.4 454.0 4540.0 45400.0

攝氏轉華氏

溫度單位
攝氏 °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

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))
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))

華氏轉攝氏(練習)

溫度單位
華氏 °F (Fahrenheit):僅剩美國在使用。
攝氏 °C (Celsius):目前大多數的國家使用的。
華氏與攝氏溫度的關係是:
C= (F – 32) * 5 / 9
輸入華氏溫度,輸出攝氏溫度。小數部分請無條件捨去。

Input

60 100 32 -20 0

Output

C=15 C=37 C=0 C=-28 C=-17

BMI(練習)

身高體重指數(英語:Body Mass Index,簡稱BMI),又稱身體質量指數,是由一個人的質量(體重)和身高計算出的一個數值。BMI的定義是體重除以身高的平方,以公斤/平方公尺為單位表示,由體重(公斤)和身高(公尺)得出:

BMI=wh2
其中
w
為體重(weight);
h
為身高(height)。

提示:

# 計算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

次方求餘

輸入三個正整數 n p d,輸出(n^p)%d,即 n 的 p次方 對 d的餘數。

Input

2 4 5

Output

1

Answer

n,p,d=map(int,input().split()) ans=(n**p)%d print(ans)

整數商餘 (作業)

輸入兩整數 m、n,輸出 m 除以 n 之 商 及 餘數。
在Python有兩種除法:

  1. 使用/運算得出商的結果是浮點數。 7/2 = 3.5
  2. 使用//運算得出商的結果是整數。7//2 = 3
  3. 用%來得到整除後的餘數,求餘數。 7%2 =1 , 7%4=3

Input

7 2 7 4 8 2

Output

3 1 1 3 4 0

Answer

#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); }

四數有權重的相加(作業)

輸入:四個數字

abcd 有權重的相加 輸出:
56a+24b+14c+6d
.

Input

1 1 1 1 1 10 100 1000

Output

100 7696

分組

要同學分組,分組的方式為依座號順序,每 3 個人一組。如:1, 2, 3 為第一組,4, 5, 6 為第二組….以此類推。輸入同學的座號,請判斷他在哪一組。

Input

輸入只有一行,含有一個正整數 n,代表同學的座號。

7
12

Output

輸出該同學的組別。

3
4
n = int(input()) print((n - 1) // 3 + 1)

錢(作業)

周杰倫A,蔡依林B和JJ,三個人都有錢。

  • 1.周杰倫A 和 蔡依林B 加起來有
    X
    元。
    A+B=X
  • 2.蔡依林B 和 JJ 加起來有
    Y
    元。
    B+JJ=Y
  • 3.周杰倫A 和 JJ 加起來有
    Z
    元。
    A+JJ=Z

每個人有多少元?
輸入:三個正偶數

XYZ數字 輸出: 周杰倫A 蔡依林B JJ ,每個人各多少元.

Input

170 160 190 168 158 188

Output

100 70 90 99 69 89


閏年 leap year,讓同學了解不同輸入格式的處理方式

除了不是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

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
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")
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
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
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
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
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")

輸出星期幾(練習)

給你 2021 年的某月某日,輸出星期幾。

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

出題模板

說明

x2

Input

1 2 1 3 4

Output

2

Answer

#Code