a, b, c, d, e, f = input(), input(), input(), input(), input(), input() print(f'{a:>10}{b:>10}{c:>10}\n{d:>10}{e:>10}{f:>10}\n{a:<10}{b:<10}{c:<10}\n{d:<10}{e:<10}{f:<10}')
108
1 2 3
rr = int(input()) r = rr / 2 print(f'{rr:<10}\n{r:<10.2f}\n{r**2*3.1415:<10.4f}')
109
1 2 3 4 5
a = int(input()) if a > 100or a < 0: print('error') else: print('pass'if a>59else'fail', 'even'if a%2==0else'odd', sep='\n')
110
1 2
a, b, c = int(input()), int(input()), int(input()) print(1if100>a>=60else0, f'{(b+1)/10:.2f}', max(a, c), sep='\n')
第2類:選擇敘述與迴圈
201
1
print('even'ifint(input())%2==0else'odd')
202
1 2
a = int(input()) print('error'if a>100else a+10if a>60else a+5)
203
if-else 解
1 2 3 4
TABLE = ('one', 'two', 'three', 'four') i = int(input())
print(TABLE[i-1] if1 <= i <= 4else'error')
204
1 2 3 4 5 6
a, b, s = int(input()), int(input()), input() if s notin ('+', '-', '*'): print('error') else: calc = eval(f'{a}{s}{b}') print(f'{a}{s}{b}={calc}')
205
1 2 3 4 5 6 7 8 9 10 11
arr = [0] * 7 for _ inrange(10): i = int(input()) if1 <= i <= 6: arr[i-1] += 1 else: arr[-1] += 1
for i inrange(6): print(f'number{i+1}:{arr[i]}') print(f'error:{arr[-1]}')
206
迴圈解
1 2
a, b = int(input()), int(input()) print(sum(i for i inrange(a, b+1) if i % 2))
公式解
1 2 3 4 5 6
defodd_sum(n): cnt = (n + 1) // 2 return cnt**2
a, b = int(input()), int(input()) print(odd_sum(b) - odd_sum(a-1))
207
1 2 3 4 5 6 7 8 9 10
defprime(n): if n <= 1: returnFalse for i inrange(2, int(n**0.5)+1): if n % i == 0: returnFalse returnTrue
n = int(input()) print(f'{n} is{""if prime(n) else" not"} a prime number')
208
1 2 3 4 5 6 7 8 9
defprime(n): if n <= 1: returnFalse for i inrange(2, int(n**0.5)+1): if n % i == 0: returnFalse returnTrue
print(*(i for i inrange(2, int(input())) if prime(i)), end=' \n')
209
1 2 3 4 5 6 7 8 9 10 11 12 13
n = int(input())
if n notin (0, 1): print('error') exit()
for i inrange(1, 6): for j inrange(1, 6): if n == 0: print(f'{i} x {j} = {i*j}', end='\t') else: print(f'{j} x {i} = {i*j}', end='\t') print()
210
1 2 3 4
from math import gcd, lcm
a, b = int(input()), int(input()) print(gcd(a, b), lcm(a, b), sep='\n')
第3類:函式與陣列
301
1 2 3 4 5 6 7
defcompute(): star, line = int(input()), int(input()) for _ inrange(line): print('*' * star) print(star*line)
compute()
302
1 2 3 4 5 6
defcompute(n): if n < 0or n > 100: return -1 return n+5if n>=60else n+10
print(compute(int(input())))
303
1 2 3 4 5 6 7 8
defcompute(n): for i inrange(2, int(n**0.5)+1): if n % i == 0: returnFalse returnTrue
n = int(input()) print(f'{n} is {""if compute(n) else"not "}a prime number')
304
1 2 3 4 5 6 7 8 9
defcompute(arr): t = 0 for i in arr: if i % 3 == 0: t += 1 return t
arr = [int(input()) for _ inrange(6)] print(compute(arr))
305
1 2 3 4 5 6
defcompute(a, b, c): returneval(a+c+b)
a, c, b = input(), input(), input() c = '+'if c=='1'else'*' print(compute(a,b,c))
306
1 2 3 4 5 6
from math import factorial
defcompute():pass#題目硬性規定要用自訂function 所以才寫這行繞過限制
n = int(input()) print(f'{n}!={factorial(n)}')
307
1 2 3
defcompute():pass
print(max(int(input()) for _ inrange(5)))
308
1 2 3 4 5
defcompute():pass
fib = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] #因為題目有說數字<=10 for i inrange(int(input()), 0, -1): print(f'fib({i})={fib[i-1]}')
309
1 2 3 4 5 6
defcompute(arr, brr): returnf'{min(a / b for a, b inzip(arr, brr)):.3f}'
arr = [int(input()) for _ inrange(3)] brr = [int(input()) for _ inrange(3)] print(compute(arr, brr))
310
1 2 3 4 5 6 7 8 9 10 11 12
defcompute(n): a = 0 for i instr(n): a += int(i)**len(str(n)) return n == a
t = 0 for i inrange(1, int(input())+1): if compute(i): print(i) t += i print(t)
withopen('read.txt') as infile, open('write.txt', 'w') as outfile: for i in infile.read(): outfile.write(chr(ord(i)+2))
410
1 2 3 4 5 6 7 8 9 10
from string import capwords #與swapcase()有點像 但不會誤把what's的s轉大寫
n = int(input())
withopen('read.txt', 'r') as infile, open('write.txt', 'w') as outfile: f = infile.readlines() for i inrange(len(f[:n])): outfile.write(capwords(f[i].strip())) if i < n-1: outfile.write('\n')
第5類:綜合應用一
501 字串轉換
1 2
s = input() print(f'{s} change to {s.split(".")[0]}')
502 數字相乘
1 2 3 4 5 6
s = input() s2 = '' for i in s: s2 += f'{i}*' s2 = s2[:-1] print(f'{s2}={eval(s2)}')
503 區間運算
1 2 3 4
a, b = int(input()), int(input()) for i inrange(1, a): if (i**0.5).is_integer(): print(int(i**0.5)**b)
504 迴文數
1 2
s = input() print('Yes'if s==s[::-1] else'No')
505 公式計算
1 2 3 4 5
from math import log10, floor
a, b, c, d, e, f = float(input()), float(input()), float(input()), float(input()), float(input()), float(input()) ans = abs(a)*floor(b) + c**d * e**0.5 + log10(f) print(f'{ans:.2f}')
ans = list(input()) for _ inrange(3): a = b = 0 g = list(input()) for i inrange(4): if ans[i] == g[i]: a += 1 elif g[i] in ans: b += 1 print(f'{a}A{b}B')
s = list(map(int, input().split('/'))) print(*s) print(sum(s))
510 星號輸出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
h, w = map(int, input().split()) m = [list(map(int, input().split())) for _ inrange(h)]
for i inrange(h): ans = '' for j inrange(w): if m[i][j] == 0: ans += ' ' else: if i==0or j==0or i==h-1or j==w-1: ans += '*' else: if m[i-1][j]==0or m[i][j-1]==0or m[i+1][j]==0or m[i][j+1]==0: ans += '*' else: ans += ' ' print(ans)
第6類:綜合應用二
601 大小寫轉換
1 2 3 4 5 6
lett = input() n = int(input())
s = lett[n].swapcase() nlet = lett[:n] + s + lett[n+1:] print(f'The letter that was selected is: {s}\n{nlet}')
602 字串拆解
1 2 3 4 5 6 7 8 9
uc = lc = '' c = 0 for i ininput(): if'A' <= i <= 'Z': uc += i c += 1 else: lc += i print(uc, lc, c, sep='\n')
603 多重迴圈
1 2 3
a, _ = map(int, input().split()) for _ inrange(a): print(input().replace(' ', ','))
for _ inrange(10): hit = int(input()) if hit == 0: continue elif hit == 1: if ans[-1]: score += 1 ans = [True] + ans[:-1] elif hit == 2: if ans[-1]: score += 1 if ans[-2]: score += 1 ans = [False, True] + ans[:-2] elif hit == 3: score += sum(ans) ans = [False, False, True] else: score += sum(ans) + 1 ans = [False] * 3
print(f'score = {score}')
609 閏年
1 2 3 4 5 6 7 8
from datetime import date #用來專門處理日期時間等問題的套件
y, m, d = map(int, input().split()) try: dt = date(y, m, d) print(dt.timetuple().tm_yday) except: print('error')
610 矩陣乘積
1 2 3 4 5 6 7 8 9 10 11 12 13
from numpy import matrix #矩陣運算用 可直接算出矩陣乘法的結果
t, _ = map(int, input().split()) a = matrix([list(map(int, input().split())) for _ inrange(t)])
t, _ = map(int, input().split()) b = matrix([list(map(int, input().split())) for _ inrange(t)])
try: for i in (a*b).tolist(): #matrix.tolist() 把它變為二維陣列 輸出才不會多括號 print(*i) except: print('error')
a, b, c = int(input()), int(input()), int(input()) s = (a+b+c) / 2 print(f'{(s*(s-a)*(s-b)*(s-c))**0.5:.2f}')
702 二進位轉十進位
1
print(int(input(), 2))
703 找零錢
1 2 3 4 5 6 7 8 9 10 11 12 13
n = int(input())
if n >= 50: print(f'{n//50}*$50', end=' ') n %= 50 if n >= 10: print(f'{n//10}*$10', end=' ') n %= 10 if n >= 5: print(f'{n//5}*$5', end=' ') n %= 5
print(f'{n}*$1')
704 過半數
1 2 3 4 5 6 7 8 9 10 11
from collections import Counter
t = int(input()) arr = Counter(map(int, input().split()))
for k, v in arr.items(): if v > t/2: print(k) break else: print('error')
705 庫存函數
1 2 3 4 5 6 7 8 9
ar = {k: int(v) for k, v in (input().split() for _ inrange(3))} #dict t = 0
for _ inrange(5): s = input() if s in ar: t += ar[s]
print(t)
706 整數檔案讀寫
1 2 3 4 5 6 7 8
arr = [int(input()) for _ inrange(4)]
withopen('read.txt', 'r') as infile, open('write.txt', 'w') as outfile: for i in infile.readlines(): arr.append(int(i.strip())) arr.sort() for i in arr: outfile.write(f'{i}\n')
707 動態記憶體配置
1 2 3 4 5 6 7 8
arr = [] for _ inrange(int(input())): a, b = map(int, input().split()) arr.append((f'{a}x{b}=', a*b))
arr.sort(key=lambda x: x[1]) for i in arr: print(*i, sep='')
708 12小時制時間
1 2 3 4 5 6 7 8 9 10 11 12 13 14
c = 0 for _ inrange(3): h, m = map(int, input().split()) if h == 0: print(f'AM 12:{m}') c += 1 elif h < 12: print(f'AM {h}:{m}') c += 1 elif h == 12: print(f'PM {h}:{m}') else: print(f'PM {h-12}:{m}') print(c)
709 圓面積計算
1 2 3 4 5 6 7 8 9 10
area = 0 biggest = [0] * 3
for _ inrange(int(input())): x, y, r = map(int, input().split()) area += r**2 * 3.14159 if r > biggest[-1]: biggest = [x, y, r]