---
# System prepended metadata

title: 全試題解答

---

# 第一回
## BMI計算
```
height = eval(input())
weight = eval(input())
x = ((height / 100) * (height / 100))
y = ((weight / x))
print (round(y,2))
```
## 107. 加總平均計算
```
sum1,sum2,sum3,sum4,sum5 = eval(input()),eval(input()),eval(input()),eval(input()),eval(input())
total = sum1+sum2+sum3+sum4+sum5
avg = total/5
print(sum1,sum2,sum3,sum4,sum5)
print(f"Sum = {total:.1f}")
print(f"Average = {avg:.1f}")
```
## 102.購買蘋果汁金額
```
apple = 23.34
buy = eval(input())
total = apple * buy
print("%.2f"%total)
```
## 104.兩個浮點數總和
```
a1=eval(input())
a2=eval(input())
a3=a1+a2
a4=str(a3)
if a4.rfind("5")==4 :
  a3 = round(a3,2)+0.01
print("total=",round(a3,2),sep="")
```
## 106.兩點距離計算
```
import math
x1 = eval(input())
y1 = eval(input())
x2 = eval(input())
y2 = eval(input())
a = math.sqrt((x1-x2)**2+(y1-y2)**2)
print("%.2f"%a)
```
# 第二回
## 202.倍數判斷
```
x = int(input())
if (x%15==0):
  print(x,"is a multiple of 3 and 5.")
elif (x%5==0):
  print(x,"is a multiple of 5.")
elif (x%3==0):
  print(x,"is a multiple of 3.")
else:
  print(x,"is not a multiple of 3 or 5.")
```
## 203. 判斷閏年
```
x=eval(input())
if(x%100==0 and x%400!=0):
  print(x,"is not a leap year.")
elif(x%4==0 or x%400==0):
  print(x,"is a leap year.")
else:
  print(x,"is not a leap year.")
```
## 206. 等級判斷
```
x=eval(input())
if(x>=80):
  print("A")
elif(x>=70 and x<=79):
  print("B")
elif(x>=60 and x<=69):
  print("C")
else:
  print("F")
```
## 207. 折扣方案
```
x=eval(input())
if(x>=38000):
  print(x*0.7)
elif(x>=28000):
  print(x*0.8)
elif(x>18000):
  print(x*0.9)
elif(x>8000):
  print(x*0.95)
else:
  print(x)
```
## 109.成績判斷
```
x=eval(input())
if(x>100 or x<0):
  print("error")
else:
  print("pass") if x>=60 else print("fail")
  print("even") if x%2==0 else print("odd")
```
## 201.奇數或偶數
```
x=eval(input())
print("even") if x%2==0 else print("odd")
```
## 202.分數輸出
```
x=eval(input())
if(x>100 or x<0):
  print("error")
else:
  print(x+10) if x>=60 else print(x+5)
```
# 第三回
## 301. 迴圈整數連加
```
start = eval(input())
end = eval(input())+1
sum = int(0)
for a in range(start,end):
  sum = sum + a
print(sum)
```
## 302. 迴圈偶數連加
```
a = eval(input())
b = eval(input())
sum = int(0)
if (a%2!=0):
  a=a+1
for i in range(a,b+1,2):
  sum = sum + i
print(sum)
```
## 304. 迴圈倍數總和
```
a=eval(input())
sum=0
for i in range(1,a+1):
  if (i%5==0):
    sum=sum+i
print(sum)
```
## 306 迴圈階乘計算
```
a=eval(input())
ans=1
if (a==0):
  print("1")
else:
  for i in range(2,a+1):
    ans=ans*i
  print(ans)
```
## 307 乘法表
```
n=eval(input())
for i in range(1,n+1):
    for a in range(1,n+1):
        ans=i*a
        print(f'{a} * {i} = {ans:<3}',end="")
    print()
```
## 310. 迴圈公式計算
```
import math
n = eval(input())
sum = 0
for i in range(1,n):
  sum = sum + (1/((math.sqrt(i)+math.sqrt(i+1))))
print(round(sum,4))
```
## 210. 最大公因數和最小公倍數
```
import math
a = eval(input())
b = eval(input())
ans1 = math.gcd(a,b)
ans2 = 9999999999999999
for i in range(1,a+1):
  for j in range(1,b+1):
    if (i*j)%a==0 and (i*j)%b==0:
      ans3 = i*j
      if i*j < ans2:
        ans2 = i*j
print(ans1)
print(ans2)
```
## 302C.迴圈檢查數字
```
a = eval(input())
b = eval(input())
ans = 0
sum = 0
for i in range(a,b+1):
  if (i%3==0 or i%5==0) and i%2!=0:
    ans = ans+1
    sum = sum+i
print("有",ans,"個",sep="")
print("總和",sum,sep="")
```
## 304. 迴圈倍數總和
```
a = eval(input())
sum = 0
for i in range(1,a+1):
  if i%5==0:
    sum = sum + i
print(sum)
```
## 308. 迴圈位數加總
```
a = eval(input())
for i in range(1,a+1):
  b = eval(input())
  total = 0
  n = 0
  while 0<=n<=9:
    d=str(b)
    s=str(n)
    c=d.count(s)
    total = total + (n * c)
    n += 1
  print("Sum of all digits of",b,"is",total)
```
## 309. 存款總額
```
n = eval(input())
p = eval(input())/1200
m = eval(input())
print("Month \t Amount")
for i in range(1,m+1):
  a = n*((1+p)**i)
  a = round(a,2)
  print(f'{i:>3} \t {a:>7.2f}')
```
## 401. 最小值
```
a=eval(input())
for i in range(1,10):
  b = eval(input())
  a=b if b<a else a
print(a)
```
## 數字倒三角形
```
l = eval(input())
for i in range(l,0,-1):
  for j in range(i,0,-1):
    print(j,end=" ")
  print()
```
## 303. 迴圈數值相乘
```
l = eval(input())
for i in range(1,l+1):
  for j in range(1,i+1):
    ans=i*j
    print(f'{ans:>4}',end="")
  print()
```
## 繪製直角三角形
```
l = eval(input())
for i in range(1,l+1):
  for j in range(1,i+1):
    print("*",end="")
  print()
```
# 第四回
### 電價計算
```
#電價計算
e = eval(input())
i = 1
total = 0
if e>0:
  if e>500:
    c = e-500
    total = 200*2 + 200*3 + c*5
  elif e>300:
    c = e-300
    total = 200*2 + c*3
  elif e>100:
    c = e-100
    total = c*2
  else:
    total=0
  print(total,"元",sep="")
else:
  print("ERROR")
```
### 408
```
#408.
totalE=0
totalO=0
for i in range(1,11):
  a = eval(input())
  if a%2==0:
    totalE += 1
  elif a%2!=0:
    totalO += 1
print(f"Even numbers:{totalE:>2}")
print(f"Odd numbers:{totalO:>2}")
```

### 高速公路超速罰款
```
#高速公路超速罰款
l = eval(input())
e = eval(input())
e = e-l
if e>0 and l>0:
  if e>60:
    total = 6000
  elif e>40:
    total = 5000
  elif e>20:
    total = 3500
  else:
    total=3000
  print("超速",e,",罰",total,"元",sep="")
elif l<0:
  print("ERROR")
elif e<0:
  print("不處罰")
```

### 209. 距離判斷
```
#209. 距離判斷
import math
x2 = eval(input())
y2 = eval(input())
a = math.sqrt((5-x2)**2+(6-y2)**2)
if a>15:
  print("Outside")
else:
  print("Inside")
```



### 406
```
#406.
height=0
weight=0
while height!=-9999 or weight!=-9999:
  height = eval(input())
  if height==-9999 or weight==-9999:
    break
  weight = eval(input())
  if height==-9999 or weight==-9999:
    break
  x = ((height / 100) * (height / 100))
  y = ((weight / x))
  BMI=(round(y,2))
  print(f"BMI: {BMI:0.2f}")
  if BMI<18.5:
    print("State: under weight")
  elif BMI<25:
    print("State: normal")
  elif BMI<30:
    print("State: over weight")
  elif BMI>=30:
    print("State: fat")
```

### 飲料自動販賣機
```
Type=str(input())
NC=eval(input())
if Type=="1":
  print("訂購紅茶",NC,"杯共",30 * NC,"元")
elif Type=="2":
  print("訂購果汁",NC,"杯共",50 * NC,"元")
elif Type=="3":
  print("訂購咖啡",NC,"杯共",80 * NC,"元")
elif Type=="4":
  print("訂購綠茶",NC,"杯共",20 * NC,"元")
else:
  print("輸入錯誤")
```
# 第五回
## 觀察串列存儲內容
```
length=eval(input())
lst1=[]
for i in range(0,length):
  lst1.insert(i,str(input()))
print(length,"筆資料,","包含:",lst1)
```
## 串列資料記錄
```
n=0
lst=[]
l=0
while n!="end":
  n=str(input())
  lst.append(n)
  l=l+1
print("共有",l-1,"筆",sep="",end="")
for i in range(0,l-1):
  print(" ",lst[i],sep="",end="")
print("")
```
## P044.判斷串列內容
```
k=eval(input())
A=eval(input())
ans=0
sum=0
for i in range(0,len(A)):
  C=A[i]
  if C%k==0:
    ans=ans+1
    sum=sum+C
print(k,"的倍數有",ans,"個",sep="")
print("總和=",sum,sep="")
```
## 603.數字排序
```
a=[]
for i in range(10):
  a.append(eval(input()))
a.sort(reverse=True)
print(a[0],a[1],a[2])
```
## 串列字串資料由小到大排序
```
a=[]
x=0
while x!="end":
  x=input()
  a.append(x)
del a[-1]
print("原始內容是",*a)
a.sort(reverse=False)
print("排序內容是",*a)
```
## 串列字串資料由大到小排序
```
a=[]
x=0
while x!="end":
  x=input()
  a.append(x)
del a[-1]
print("原始內容是",sep="",end="")
for i in range(0,len(a)):
  print(" ",a[i],sep="",end="")
a.sort(reverse=False)
print()
print("排序內容是",sep="",end="")
for i in range(0,len(a)):
  print(" ",a[i],sep="",end="")
```
## 308B.判斷串列內容
```
A=[780,372,216,685,975,232,110,867,95,14,179,855,450,758,280,703,585,112,212,212,349,333,372,305,928,673,604,677,1,978,493,84,544,215,642,538,726,906,379,902,184,612,801,254,64,326,986,5,781,891]
sum=0
ans=0
for i in A:
  if i%7==0:
    ans=ans+1
    sum=sum+i
a=input()
a=input()
print(ans,"個\n總和",sum)
```
# 第六回
## 502. 乘積
```
def compute(x,y):
  return x*y
x=eval(input())
y=eval(input())
print(compute(x,y))
```
## 503. 連加計算
```
def compute(x,y):
  sum=0
  for i in range(x,y+1):
    sum=sum+i
  return sum

x=eval(input())
y=eval(input())
print(compute(x,y))
```
## 504. 次方計算
```
def compute(x,y):
  return pow(x,y)
x=eval(input())
y=eval(input())
print(compute(x,y))
```
## 507. 質數
```
def compute(x):
  s=0
  for i in [2,3,5,7,11,13,17]:
    if x/i==1:
      s=1
  return s

x=eval(input())
if compute(x)==1:
  print("Prime")
else:
  print("Not Prime")
```
## 510. 費氏數列
```
def compute(x):
  if x==0:
    return 0
  if x==1:
    return 1
  if x>=2:
    return compute(x-2)+compute(x-1)

x=eval(input())
for i in range(0,x):
  print(compute(i),"",end="")
```
## 函數回傳2個結果
```
def f(x,y):
  print(f"{x}+{y}=",x+y,sep="")
  print(f"{x}-{y}=",x-y,sep="")
x=eval(input())
y=eval(input())
f(x,y)
```
## 302.期中考分數
```
def compute(score):
  if score<0 or score>100:
    print("-1")
  elif score>=60:
    print(score+5)
  else:
    print(score+10)

score=eval(input())
compute(score)
```
## 304.判斷有幾個3的倍數
```
def compute(lst1):
  y=0
  for i in range(6):
    if lst1[i]%3==0:
      y=y+1
  return y

lst1=[]
for i in range(6):
  x=eval(input())
  lst1.append(x)
print(compute(lst1))
```
## 306.n階計算
```
def compute(x):
  if x==0:
    return 1
  if x>=1:
    return x*compute(x-1)
x=eval(input())
print(x,"!=",compute(x),sep="")
```
## 計算成績總和與平均
```
def sum(a,b,c,d,e):
  f=(a+b+c+d+e)/5
  print(f"total = {a+b+c+d+e}\naverage = {f}")
a=eval(input())
b=eval(input())
c=eval(input())
d=eval(input())
e=eval(input())
sum(a,b,c,d,e)
```
## 305.數值運算
```
def compute(index,x,y):
    if index==1:
        ans=x+y
    if index==2:
        ans=x*y
    return ans
x=eval(input())
index=eval(input())
y=eval(input())
print(compute(index,x,y))
```
## 506. 一元二次方程式
```
ans=0
ant=0
def compute(a,b,c):
    r=(b**2)-(4*a*c)
    if r<0 or a==0 or b==0 or c==0:
        return "Your equation has no root."
    else:
        ans=((b-b*2)+(r**0.5))/(2*a)
        ant=((b-b*2)-(r**0.5))/(2*a)
        if ans==ant:
           return str(ans)
        else:
           return str(ans)+", "+str(ant)

a=eval(input())
b=eval(input())
c=eval(input())
print(compute(a,b,c))
```
# 考試
## 01. 串列總和
```
a=[]
sum=0
while True:
  try:
    n=int(input())
    a.append(n)
    sum=sum+n
  except:
    print("内容是",end="")
    for i in range(len(a)):
      print(f"(a[i])",end="")
    print(f",總和 {sum}")
    break
```
## 02.函數區間連加計算
```
def f(a,b):
  sum=0
  for i in range(a,btl):
    sum=sum+i
  return sum

a=int(input())
b=int(input())
print(f(a,b))
```
## 03.求兩數之間所有3倍數或7倍數的和
```
def f(a,b):
  sum=0
  for i in range(b,a-1,-1):
    if 1%3==0 or 1%7==0:
      sum=sum+1
  return sum

a=int(input())
b=int(input())
print(f(b,a)) if a>b else print(f(a,b))
```
## 04.停車費問題
```
time=int(input())
ans=0
if time<30:
  ans=0
if time>30:
  if time%30==0:
    ans=int(time/30-1)*15
  else:
    ans=int(int(time/30)*15)
  if ans>240:
    ans=240
print(ans)
```
## 05.303.函數質數判斷
```
def compute(n):
  if n==2 or n==3 or n==5 or n==7:
    return f"{n} is a prime number"
  elif n%2!=0 and n%3!=0 and n%5!=0 and n%7!=0:
    return f"{n} is a prime number"
  else:
    return f"{n} is not a prine number"

n=int(input())
print(compute(n))
```
## 06. 工讀生薪資問題
```
time=int(input())
ans=0
if time>120:
  ans=60*180+60*180*1.25+(time-120)*180*1.5
elif time>60:
  ans=60*180+(time-60)*180*1.25
else:
  ans=time*180
print(ans)
```
## 07. 405. 不定數迴圈-分數等級
```
while True:
  try:
    s=int(input())
    if s==-9999:
      raise
    if s>=90:
      print("A")
    elif s>=80:
      print("B")
    elif s>=70:
      print("C")
    elif s>=60:
      print("D")
    elif s<60:
      print("E")
  except:
    break
```
## 08. 602. 撲克牌總和
```
sum=0
while True:
  try:
    joker=str(input())
    if joker=="K":
      joker=13
    if joker=="Q":
      joker=12
    if joker=="J":
      joker=11
    if joker=="A":
      joker=1
    sum=sum+int(joker)
  except:
    print(sum)
    break
```