#TQC-Python 1
#8月2日 https://www.facebook.com/buddin5678/posts/pfbid025PJyB7QKfv7nSyK7ZARHmBFYJ2FwJiR2kx5e13ydgmBPmTXt4z18hojs55vk9poTl
#PYA102.py 格式化輸出
#23.12 395.3 100.4617 564.329
'''
a = float(input())
b = float(input())
c = float(input())
d = float(input())
print('|%7.2f %7.2f|' % (a,b))
print('|%7.2f %7.2f|' % (c,d))
print('|%-7.2f %-7.2f|' % (a,b))
print('|%-7.2f %-7.2f|' % (c,d))
#- -
#PYA104.py 計算圓面積
#10 2.5
import math
r = float(input())
print(f'Radius = {r:.2f}')
print(f'Perimeter = {2 * math.pi * r:.2f}')
print(f'Area = {math.pi * r * r:.2f}')
#- -
#PYA106.py 公里英哩換算
#10 25 3
x = int(input())
y = int(input())
z = int(input())
print(f'Speed = {z/1.6/(x*60+y)*3600:.1f}')
#- -
#PYA108.py 座標距離計算
#2 1 5.5 8
x1 = eval(input())
y1 = eval(input())
x2 = eval(input())
y2 = eval(input())
print(f'( {x1} , {y1} )')
print(f'( {x2} , {y2} )')
D = ((x1-x2)**2 + (y1-y2)**2)**0.5
print(f'Distance = {D:.4f}')
#- -
#PYA110.py 正n邊形面積計算
#8 6
from math import *
n = int(input())
s = int(input())
print(f'Area = {n*s**2/( 4*tan(pi/n) ):.4f}')
#- -
#PYA202.py 倍數判斷
#55 36 92 15
a = int(input())
j = (a % 3 == 0) + (a % 5 == 0) * 2
fn1 = f'{a} is not a multiple of 3 or 5.'
fn2 = f'{a} is a multiple of 3.'
fn3 = f'{a} is a multiple of 5.'
fn4 = f'{a} is a multiple of 3 and 5.'
print( (fn1,fn2,fn3,fn4)[j] )
#- -
a = int(input())
j = (a % 3 == 0) + (a % 5 == 0) * 2
b = ["3 or 5","3","5","3 and 5"]
print(f'{a} is{[""," not"][j==0]} a multiple of {b[j]}.')
#- -
#204 算術運算
#30 20 *
a = int(input())
b = int(input())
OP = input()
j = (OP == '+') + (OP == '-')*2 + (OP == '*')*3 + \
(OP == '/')*4 + (OP == '//')*5 + (OP == '%')*6
y = ('',a+b,a-b,a*b,a/b,a//b,a%b)
print(y[j])
#- -
#206 等級判斷
#79 B
s = int(input())
print('FCBA'[(s>59) + (s>69) + (s>79)])
#- -
#208 十進位換算
#13 D, 8 8
n = int(input())
print(hex(n)[2:].upper())
#- -
#210 三角形判斷
#5 6 13
a = int(input())
b = int(input())
c = int(input())
v = (a+b-c)*(a-b+c)*(-a+b+c)
print(["Invalid",a+b+c][v>0])
#- -
#302 迴圈偶數連加
#14 1144 327714
a = int(input())
b = int(input())
print(sum(list(range(a+a%2,b+1,2))))
#- -
#304 迴圈倍數總和
#21 50
n = int(input())
print(sum(list( range(n//5+1) ))*5)
#print(sum(list(range(5,int(input())+1,5))))
#- -
#306 迴圈階乘計算
#15 1307674368000
n = int(input())
prod = 1
for i in range(2,n+1): prod *= i
print(prod)
#- -
#308 迴圈位數加總
freq = int(input())
for i in range(freq):
num = input()
sum = 0
for i in range(len(num)): sum += int(num[i])
print(f'Sum of all digits of {num} is {sum}')
#- -
for i in range(int(input())):
num = input()
sum = 0
for i in range(len(num)): sum += int(num[i])
print(f'Sum of all digits of {num} is {sum}')
#- -
#310 迴圈公式計算
#8 1.8284
n = int(input())
a = 1
sum = 0
for i in range(n-1):
b = (i+2)**0.5
sum += 1/(a+b)
a = b
print(f'{sum:.4f}')
#- -
n = int(input())
sum = 0
for i in range(1,n):
sum += 1/(i**0.5+(i+1)**0.5)
print(f'{sum:.4f}')
#- -
#402 不定數迴圈-最小值
m,n = 0,0
while n != 9999:
n = int(input())
if m > n: m = n
print(m)
#- -
x = [0]
while x[-1] != 9999:
x += [int(input())]
print(min(x[1:]))
#- -
#404 數字反轉判斷
print(input()[::-1])
#- -
#406 不定數迴圈-BMI計算
while True:
h = int(input())/100
if h == -99.99: break
w = int(input())
BMI = w/h**2
a = (BMI>=18.5) + (BMI>=25) + (BMI>=30)
state = ('under weight','normal','over weight','fat')
print(f'BMI: {BMI:.2f}')
print(f'State: {state[a]}')
#- -
h = int(input())
x = ["under weight","normal","over weight","fat"]
while h != -9999:
w = int(input())
BMI = w/(h/100)**2
print(f'BMI: {BMI:.2f}')
y = (BMI>=18.5) + (BMI>=25) + (BMI>=30)
print(f'State: {x[y]}')
h = int(input())
#- -
h = int(input())
x = ('fat','over weight','normal','under weight')
while h != -9999:
w = int(input())
BMI = w/(h/100)**2
print(f'BMI: {BMI:.2f}')
y = (BMI<18.5) + (BMI<25) + (BMI<30)
print(f'State: {x[y]}')
h = int(input())
#- -
#408 奇偶數個數計算
odd = 0
for i in range(10):
odd += int(input()) % 2
print(f'Even numbers: {10-odd}')
print(f'Odd numbers: {odd}')
#- -
#410 繪製等腰三角形
n = int(input())
for i in range(n): print(f'{" "*(n-1-i)}{"*"*(2*i+1)}')
#- -
n = int(input())
for i in range(n):
print(' '*(n-i-1),end='')
print('*'*(i*2+1))
#- -
#502 乘積
def compute(a,b): print(a*b)
compute(int(input()),int(input()))
#- -
#504 次方計算
def compute(a,b): print(a**b)
compute(int(input()),int(input()))
#- -
#506 一元二次方程式
def compute(a,b,c):
D = b**2 - 4*a*c
if D < 0: print('Your equation has no root.')
elif not D: print(-b/(2*a))
else: print( (-b+D**0.5)/(2*a),(-b-D**0.5)/(2*a),sep=', ')
a = int(input())
b = int(input())
c = int(input())
compute(a,b,c)
#- -
def compute(a,b,c):
D = b**2-4*a*c
if D < 0:
print('Your equation has no root.')
elif D == 0:
print(b/(2*a))
else:
u = (-b+D**0.5)/(2*a)
v = (-b-D**0.5)/(2*a)
print(f'{u}, {v}')
a = int(input())
b = int(input())
c = int(input())
compute(a,b,c)
#- -
#508 最大公因數*
def compute(a,b):
while b: a, b = b, a % b
return a
a,b = map(int, input().split(','))
print(compute(a,b))
#- -
#510 費氏數列
def compute(n):
a,b,x = 0,1,'0 1 '
for i in range(n-2):
a,b = b, a+b
x += f'{b} '
print(x)
n = int(input())
compute(n)
#- -
def compute(n):
a,b=0,1
print(a,b,end=' ')
for i in range(n-2):
a,b=b,a+b
print(b,end=' ')
compute(int(input()))
#- -
def fib(n): # 建立函式 fib,帶有參數 n
if n > 1: # 如果 n 大於 1
return fib(n-1) + fib(n-2) # 使用遞迴
return n
for i in range(20): # 產生 20 個數字
print(fib(i), end = ',')
#- -
#602 撲克牌總和
x = ('A','2','3','4','5','6','7','8','9','10','J','Q','K')
D = dict( zip(x, list(range(1,14)) ) )
a = 0
for i in range(5): a += D[input()]
print(a)
#- -
i,a = 0,0
x = ('A','2','3','4','5','6','7','8','9','10','J','Q','K')
D = dict( zip(x, list(range(1,14)) ) )
while i < 5:
n = input()
if n:
a += D[n]
i += 1
print(a)
#- -
x = 'AJQK'
sum = 0
for i in range(5):
n = input()
if n in x: sum += (1,11,12,13)[x.find(n)]
else: sum += int(n)
print(sum)'''
#- -
#604 眾數
i = 0
x,y = [],[]
while i < 10:
try:
a = 1
n = int(input())
if n in x: a += x.count(n)
x += [n]
y += [a]
i += 1
except: pass
f = max(y)
print(x[y.index(f)])
print(f)
#- -
x,y = [],[]
for i in range(10):
a = 1
n = int(input())
if n in x: a += x.count(n)
x += [n]
y += [a]
f = max(y)
print(x[y.index(f)])
print(f)
#- -
# Python 605 成績計算
i = 0
x = []
while i < 10:
try:
n = int(input())
x += [n]
i += 1
except: pass
s = sum(x) - max(x) - min(x)
print(s)
print(f'{s/8:.2f}')
#- -
Python 606 二維串列行列數
def compute(rows,cols):
for row in range(rows):
for col in range(cols):
print(f'{col - row:4d}', end='')
print()
rows = int(input())
cols = int(input())
compute(rows,cols)
#- -
def compute(r,c):
for i in range(r):
for j in range(c):
print(f'{j-i:4d}',end='')
print()
r = int(input())
c = int(input())
compute(r,c)
#- -
x = [] #Python 607 成績計算
y = ('1st','2nd','3rd')
for i in range(3):
print(f'The {y[i]} student:')
for j in range(5):
n = int(input())
x += [n]
for i in range(3):
print(f'Student {i+1}')
a = 5*i
s = sum(x[a:a+5])
print(f'#Sum {s}')
print(f'#Average {s/5:.2f}')
#- -
i = 0 #Python 608 最大最小值索引
x = []
while i < 9:
n = input()
if n:
x += [int(n)]
i += 1
a = max(x)
a1,a2 = divmod( x.index(a),3)
b = min(x)
b1,b2 = divmod( x.index(b),3)
print(f'Index of the largest number {a} is: ({a1}, {a2})')
print(f'Index of the smallest number {b} is: ({b1}, {b2})')
#- -
x = []
for i in range(9):
x += [int(input())]
L,s = max(x),min(x)
a,b = x.index(L),x.index(s)
print( f'Index of the largest number {L} is: ({a//3}, {a%3})')
print(f'Index of the smallest number {s} is: ({b//3}, {b%3})')
#- -
x = [] #Python 609 矩陣相加
for i in range(1,3):
print(f'Enter matrix {i}:')
for j in range(1,3):
for k in range(1, 3):
n = int(input(f'[{j}, {k}]: '))
x += [n]
for i in range(2):
print(f'Matrix {i+1}:')
for j in range(2):
for k in range(2):
print(x[i*4+j*2+k], end=' ')
print()
print(f'Sum of 2 matrices:')
for j in range(2):
for k in range(2):
print(x[j*2+k] + x[4+j*2+k], end=' ')
print()
#- -
#Python 610 平均溫度
x = []
for i in range(4):
print(f'Week {i+1}:')
for j in range(3):
x += [eval(input(f'Day {j+1}:'))]
print(f'Average: {sum(x)/12:.2f}')
print(f'Highest: {max(x)}')
print(f'Lowest: {min(x)}')
x = [] #Python 702 數組合併排序
for i in range(2):
print(f'Create tuple{i+1}:')
n = 0
while n != -9999:
n = input()
if n:
n = int(n)
x += [n]
if n == -9999: x.pop()
print(f'Combined tuple before sorting: {tuple(x)}')
print(f'Combined list after sorting: {sorted(x)}')
y = []
for i in range(2):
print(f'Create tuple{i+1}:')
x = [0]
while x[-1] != -9999:
x += [int(input())]
y += x[1:-1]
print(f'Combined tuple before sorting: {tuple(y)}')
print(f'Combined list after sorting: {sorted(y)}')
x = [0]
for i in range(2):
print(f'Create tuple{i+1}:')
while x[-1] != -9999:
x += [int(input())]
x.pop()
print(f'Combined tuple before sorting: {tuple(x[1:])}')
print(f'Combined list after sorting: {sorted(x[1:])}')
#Python 704 集合條件判斷
x = set()
n = 0
while n != -9999:
n = int(input())
x |= {n}
x.remove(n)
print(f'Length: {len(x)}')
print(f'Max: {max(x)}')
print(f'Min: {min(x)}')
print(f'Sum: {sum(x)}')
#Python 706 全字母句
x = 'abcdefghijklmnopqrstuvwxyz'
n = int(input())
i = 0
while i < n:
y = input()
if y:
y = y.lower()
k = 0
for j in x:
if j not in y:
print(False)
k = 1
break
if not k:
print(True)
i += 1
x = 'abcdefghijklmnopqrstuvwxyz'
for i in range(int(input())):
y = input().lower()
k = 0
for j in x:
if j not in y:
print(False)
k = 1
break
if not k:
print(True)
for i in range(int(input())):
y = set(input().lower())
if len(y) == 27:
print(True)
else:
print(False)
#Python 708 詞典合併
x = {}
for i in range(2):
print(f'Create dict{i+1}:')
k = input('Key: ')
while k != 'end':
x[k] = input('Value: ')
k = input('Key: ')
y = sorted(list(x))
for i in range(len(y)):
print(f'{y[i]}: {x[y[i]]}')
x = {} #Python 710 詞典搜尋
while True:
k = input('Key: ')
if k == 'end': break
x[k] = input('Value: ')
if input('Search key: ') in x: print(True)
else: print(False)
x = {}
k = input('Key: ')
while k != 'end':
x[k] = input('Value: ')
k = input('Key: ')
if input('Search key: ') in x:
print(True)
else:
print(False)
sum = 0 #Python 802 字元對應
s = input()
for i in range(len(s)):
n = ord(s[i])
print(f"ASCII code for '{s[i]}' is {n}")
sum += n
print(sum)
#- -
s = input() #Python 804 大寫轉換
print(s.upper())
print(s.title())
#- -
#Python 806 字元次數計算
def compute(s1): print(f'{s1} occurs {s.count(s1)} time(s)')
s = input()
a = 0
while a < 1:
s1 = input()
if s1:
compute(s1)
a += 1
def compute(s1): print(f'{s1} occurs {s.count(s1)} time(s)')
s = input()
compute(input())
def compute(s,u):
print(f'{u} occurs {s.count(u)} time(s)')
compute(input(),input())
#- -
a = 0 #Python 808 社會安全碼
SSN = input()
for i in (0,1,2,4,5,7,8,9,10):
if not SSN[i].isdigit():
print('Invalid SSN')
a = 1
break
if not a:
print('Valid SSN')
#- -
n = int(input()) #Python 810 最大值與最小值之差
for i in range(n):
a = 0
while a < 1:
x = list(map(float, input().split()))
if x:
print(f'{max(x)-min(x):.2f}')
a += 1
n = int(input())
for i in range(n):
x = list(map(float,input().split()))
print(f'{max(x)-min(x):.2f}')
#- -
#Python 902 資料加總
with open('read.txt') as fn: xs = fn.read()
x = xs.split()
sum = 0
for i in range(len(x)):
sum += int(x[i])
print(sum)
#- -
#Python 904 資料計算
# -*- coding: UTF-8 -*-
with open('read.txt') as fn: xs = fn.read()
x = xs.split()
n,h,w = [],[],[]
b = len(x) // 3
x1 = xs.split('\n')
for i in range(b):
print(x1[i])
n += [x[i*3]]
h += [int(x[i*3+1])]
w += [int(x[i*3+2])]
if i == b-1: break
print()
h1 = max(h)
w1 = max(w)
print(f'Average height: {sum(h)/b:.2f}')
print(f'Average weight: {sum(w)/b:.2f}')
print(f'The tallest is {n[h.index(h1)]} with {h1:.2f}cm')
print(f'The heaviest is {n[w.index(w1)]} with {w1:.2f}kg')
#- -
#Python 904 資料計算
# -*- coding: UTF-8 -*-
f = open('read.txt','r')
y = f.readlines()
n,h,w = [],[],[]
for xs in y:
print(xs)
x = xs.split()
n += [x[0]]
h += [float(x[1])]
w += [float(x[2])]
h1 = max(h)
w1 = max(w)
print(f'Average height: {sum(h)/len(h):.2f}')
print(f'Average weight: {sum(w)/len(w):.2f}')
print(f'The tallest is {n[h.index(h1)]} with {h1:.2f}cm')
print(f'The heaviest is {n[w.index(w1)]} with {w1:.2f}kg')
f.close()
#- -
#Python 906 字串資料取代
with open(input()) as fn: xs = fn.read()
while True:
s1 = input()
if s1: break
while True:
s2 = input()
if s2: break
print('=== Before the replacement')
print(xs)
print('=== After the replacement')
print( xs.replace(s1,s2) )
#- -
with open(input()) as fn: xs = fn.read()
s1 = input()
s2 = input()
print('=== Before the replacement')
print(xs)
print('=== After the replacement')
print( xs.replace(s1,s2) )
#- -
#Python 908 單字次數計算
with open(input()) as fn: xs = fn.read()
x = xs.split()
n = int(input())
y = []
for i in set(x):
if x.count(i) == n:
y += [i]
for i in sorted(y):
print(i)
#- -
#Python 910 學生基本資料
with open('read.dat',encoding='UTF-8') as fn: xs = fn.read()
x = xs.split('\n')
for i in x:
print(i)
if i == x[-1]: break
print()
y = []
x1 = xs.split()
b = len(x1) // 4
n = 0
for i in range(1,b):
n += int(x1[i*4+2])
print(f'Number of males: {n}')
print(f'Number of females: {b-n-1}')
#- -
with open('read.dat',encoding='UTF-8') as fn: xs = fn.read()
x = xs.split('\n')
for i in x:
print(i)
if i == x[-1]: break
print()
x1 = xs.split()
print(f'Number of males: {x1.count("1")}')
print(f'Number of females: {x1.count("0")}')
xs = '' #Python 901 成績資料
i = 0
while i < 5:
L = list( map(str,input().split()) )
if L:
if i == 4:
xs += f'{L[0]} {L[1]}'
break
xs += f'{L[0]} {L[1]}"\n"'
i += 1
xs = xs.replace('"','')
with open('write.txt','w') as fn: fn.write(xs)
#- -
i = 0 #Python 903 成績資料
x = ''
while i < 5:
n = input()
if n:
x += f'\n{n}'
i += 1
with open('data.txt','a') as fn: fn.write(x)
print('Append completed!')
print('Content of "data.txt":')
with open('data.txt') as fn: xs = fn.read()
print(xs)
#- -
#Python 905 字串資料刪除
with open(input()) as fn: xs = fn.read()
while True:
s1 = input()
if s1: break
print('=== Before the deletion')
print(xs)
print('=== After the deletion')
print(xs.replace(s1,''))
#- -
書9-28
#Python 907 詳細資料顯示
with open(input()) as fn: xs = fn.read()
x = xs.split('\n')
j = 0
for k in x:
if k: j += 1
print(f'{j} line(s)')
x1 = xs.split()
print(f'{len(x1)} word(s)')
x2 = xs.replace(' ','')
a = xs.count('\n')
print(f'{len(x2)-a} character(s)')
#- -
x = '' #Python 909 聯絡人資料
i = 0
while i < 5:
L = list( map(str,input().split()) )
if L:
if i == 4:
x += f'{L[0]} {L[1]}'
break
x += f'{L[0]} {L[1]}\n'
i += 1
with open('data.dat','w') as fn: fn.write(x)
with open('data.dat') as fn: x1 = fn.read()
print('The content of "data.dat":')
x2 = x1.split('\n')
for j in x2:
print(j)
print()
x = ''
i = 0
while i < 5:
L = list( map(str,input().split()) )
if i == 4:
x += f'{L[0]} {L[1]}'
break
x += f'{L[0]} {L[1]}\n'
i += 1
with open('data.dat','w') as fn: fn.write(x)
with open('data.dat') as fn: x1 = fn.read()
print('The content of "data.dat":')
x2 = x1.split('\n')
for j in x2:
print(j)
print()
#- -
s = input() #801
for i in range(len(s)):
print(f"Index of '{s[i]}': {i}")
#- -
s = input().split() #803
print(s[-3],s[-2],s[-1])
#- -
s = input() #805
print(f'|{s:<10s}|')
print(f'|{s:^10s}|')
print(f'|{s:>10s}|')
#- -
n = input().split() #807
s = 0
for i in n:
s += int(i)
print(f'Total = {s}')
print(f'Average = {s/len(n):.1f}')
#- -
import re #Python 809 密碼規則
s = input()
rule = re.compile('[a-z0-9]*[A-Z]+[a-z0-9]*')
msg = rule.search(s)
if not msg or len(msg[0]) < 8: print('Invalid password')
else: print('Valid password')
#競賽
N = int(input())
x = []
for i in range(N):
n = int(input(' '))
x += [n]
print(sum(x)-min(x))
請撰寫一程式,讓使用者輸入三個正整數,分別為西元年、月、日,請計算此日期為當年的第幾天,需注意閏年;若輸入的日期有誤,請輸出「error」。 閏年: 西元年份除以4不可整除,為平年。 西元年份除以4可整除,且除以100不可整除,為閏年。 西元年份除以100可整除,且除以400不可整除,為平年。 西元年份除以400可整除,為閏年。 輸入說明 三個正整數,分別為西元年、月、日 輸出說明 此日期為當年的第幾天
Jul 14, 2023https://web.ntnu.edu.tw/~algo/Prime.html #include <stdio.h> //4 #include <math.h> #define N 100 //20000000 int sieve[N]={1,1}; // 1代表不是質數, 0代表是質數 void main() //eratosthenes { // 只需要刪掉sqrt(N)以下的質數的倍數。 for (int i=2; i<=sqrt(N); i++) if (!sieve[i])
Jun 20, 2023// Fig. 10.3: fig10_03.c // Card shuffling and dealing program using structures #include <stdio.h> #include <stdlib.h> #include <time.h> #define CARDS 52 #define FACES 13 struct card {const char *face,*suit;};
Jun 20, 2023//https://liuxiaozhu.github.io/algorithm-test/AlgorithmGossip/KnightTour.htm #include <stdio.h> #define N 8 int travel(int x, int y); int board[N][N] = {0}; int main(void) { travel(5, 6); //起始點 for(int i=0; i<N; i++) { for(int j=0; j<N; j++) printf("%3d", board[i][j]);
Apr 2, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up