101 整數格式化輸出
說明: 請撰寫一程式,輸入四個整數,然後將這四個整數以欄寬為5、欄與欄間隔一個空白字元,再以每列印兩個的方式,先列印向右靠齊,再列印向左靠齊,左右皆以直線 |(Vertical bar)作為邊界。
輸入
85 4 299 478
輸出
程式碼:
x0=int(input()) x1=int(input()) x2=int(input()) x3=int(input()) print(f"|{x0:>5d} {x1:>5d}|") print(f"|{x2:>5d} {x3:>5d}|") print(f"|{x0:<5d} {x1:<5d}|") print(f"|{x2:<5d} {x3:<5d}|")
102 浮點數格式化輸出
說明:
請撰寫一程式,輸入四個分別含有小數1到4位的浮點數,然後將這四個浮點數以欄寬為7、欄與欄間隔一個空白字元、每列印兩個的方式,先列印向右靠齊,再列印向左靠齊,左右皆以直線 |(Vertical bar)作為邊界。
輸出浮點數到小數點後第二位。
23.12 395.3 100.4617 564.329
輸出
程式碼:
x0=float(input()) x1=float(input()) x2=float(input()) x3=float(input()) print(f"|{x0:>7.2f} {x1:>7.2f}|") print(f"|{x2:>7.2f} {x3:>7.2f}|") print(f"|{x0:<7.2f} {x1:<7.2f}|") print(f"|{x2:<7.2f} {x3:<7.2f}|")
103 字串格式化輸出
說明:
請撰寫一程式,輸入四個單字,然後將這四個單字以欄寬為10、欄與欄間隔一個空白字元、每列印兩個的方式,先列印向右靠齊,再列印向左靠齊,左右皆以直線 |(Vertical bar)作為邊界。
輸入
How do you turn
輸出
程式碼:
x0=input() x1=input() x2=input() x3=input() print(f"|{x0:>10} {x1:>10}|") print(f"|{x2:>10} {x3:>10}|") print(f"|{x0:10} {x1:10}|") print(f"|{x2:10} {x3:10}|")
104 圓形面積計算
說明:
請撰寫一程式,輸入一圓的半徑,並加以計算此圓之面積和周長,最後請印出此圓的半徑(Radius)、周長(Perimeter)和面積(Area)。
需import math模組,並使用math.pi。
輸出浮點數到小數點後第二位。
輸入
15
輸出
程式碼:
import math x=float(input()) Radius =x Perimeter=2*math.pi*Radius Area=math.pi*(Radius*Radius) print(f"Radius = {Radius:.2f}") print(f"Perimeter = {Perimeter:.2f}") print(f"Area = {Area:.2f}")
105 矩形面積計算
說明:
輸入
22.5 15
輸出
程式碼:
import math x=float(input()) y=float(input()) Height=x Width=y Perimeter=2*x+2*y Area=x*y print(f"Height = {Height:.1f}") print(f"Width = {Width:.1f}") print(f"Perimeter = {Perimeter:.1f}") print(f"Area = {Area:.1f}")
106 公里英里時速換算
說明:
輸入
10
30
3
輸出
程式碼:
import math x=float(input()) y=float(input()) z=float(input()) z=z/1.6 Average=z/((60*x+y)/3600) print(f"Speed = {Average:.1f}")
107 數值計算
說明:
請撰寫一程式,讓使用者輸入五個數字,計算並輸出這五個數字之數值、總和及平均數。
輸入
10
20
40
30
3
輸出
程式碼:
import math x1=float(input()) x2=float(input()) x3=float(input()) x4=float(input()) x5=float(input()) print(f"{x1} {x2} {x3} {x4} {x5}") Sum=x1+x2+x3+x4+x5 print(f"Sum = {Sum:.1f}") Average=(Sum/5) print(f"Average = {Average:.1f}")
108 座標距離計算
說明:
請撰寫一程式,讓使用者輸入四個數字x1、y1、x2、y2,分別代表兩個點的座標(x1, y1)、(x2, y2)。計算並輸出這兩點的座標與其歐式距離。
兩座標的歐式距離,輸出到小數點後第四位
輸入
2
1
5.5
8
輸出
程式碼:
import math x1=float(input()) y1=float(input()) x2=float(input()) y2=float(input()) print('(%.1f,%.1f)'%(x1,y1)) print('(%.1f,%.1f)'%(x2,y2)) dis=(((x1-x2)**2+(y1-y2)**2)**0.5) print(f"Distance = {dis:.4f}")
109 正五邊形面積計算
說明:
請撰寫一程式,讓使用者輸入一個正數s,代表正五邊形之邊長,計算並輸出此正五邊形之面積(Area)。
建議使用import math模組的math.pow及math.tan
正五邊形面積的公式:
輸出浮點數到小數點後第四位
輸入
9
輸出
程式碼:
import math s=int(input()) area=(5*pow(s,2))/(4*math.tan(math.pi/5)) print(f"Area = {area:.4f}")
110 正n邊形面積計算
說明:
請撰寫一程式,讓使用者輸入兩個正數n、s,代表正n邊形之邊長為s,計算並輸出此正n邊形之面積(Area)。
建議使用import math模組的math.pow及math.tan
正n邊形面積的公式:
輸出浮點數到小數點後第四
輸入
9
5
輸出
程式碼:
import math n=int(input()) s=int(input()) print(f'{(n * math.pow(s, 2)) / (4 * math.tan(math.pi / n)):.4f}')
系列文章: