###### tags: `Python 教學` # 課程小筆記 ## 題目1 Hello World ### 描述 ``` 輸入:無輸出 輸出:請出輸出Hello World ``` ### 測資 輸入1: ``` No Input ``` 輸出1: ``` Hello World ``` ### 範例程式 ```python= print("Hello World") ``` ### 練習類題 https://zerojudge.tw/ShowProblem?problemid=a001 ## 題目2 兩數相減 ### 描述 ``` 輸入兩個數字,輸出兩數相減 輸入:兩數用換行分開 輸出:請出輸出兩數相減 ``` ### 測資 輸入1: ``` 12 23 ``` 輸出1: ``` -11 ``` ### 範例程式 ```python= a = int(input()) b = int(input()) print(a-b) ``` ### 練習類題 https://zerojudge.tw/ShowProblem?problemid=a002 ## 題目3 判斷是否為等差數列 ### 等差數列定義 ``` 每兩個相隔數字相差數字相同 ``` ### 描述 ``` 輸入三個數字,輸出是否為等差數列 輸入:輸入三個數字(一行輸入) 輸出:True(是等差),False(不是等差) ``` ### 測資 輸入1: ``` 10 20 30 ``` 輸出1: ``` True ``` 輸入2: ``` 10 20 35 ``` 輸出2: ``` False ``` ### 範例程式 ```python= b = input() a1,a2,a3 = b.split() print(a1-a2==a2-a3) ``` ### 練習類題 https://zerojudge.tw/ShowProblem?problemid=a005 ## 題目4 回文判斷簡單版 ### 回文定義 ``` 從左讀跟從右讀是一樣的 ``` ### 描述 ``` 輸入一個文字,輸出是否為回文 輸入:輸入一個文字(必為偶數字數) 輸出:True(是回文),False(不是回文) ``` ### 測資 輸入1: ``` abba ``` 輸出1: ``` True ``` 輸入2: ``` abcdba ``` 輸出2: ``` False ``` ### 範例程式 ```python= a = input() b = True for i in range(len(a/2)): b = b and a[i]==a[-i-1] print(b) ``` ### 練習類題 https://zerojudge.tw/ShowProblem?problemid=a022 ## 題目5 分數計算簡單版 ### 描述 ``` 輸入:給五個行文字,每行三個數字分別代表每個部分對的題數,總題數,每題配分 輸出:分數以及正確率(正確數/總題數) ``` ### 測資 輸入1: ``` 1 10 10 2 20 20 3 30 30 4 40 40 5 50 50 ``` 輸出1: ``` True ``` ### 範例程式 ```python= All = 0 correct = 0 score = 0 for i in range(5): a = input().split() score+= int(a[0])*int(a[2]) correct += a[0] All += int(a[1]) print(score,correct/All) ``` ### 練習類題 https://zerojudge.tw/ShowProblem?problemid=a053 ## 題目6 彩色蘿蔔簡單版 ### 描述 ``` 在一個神奇的國度裡,有一種兔子,它只吃蘿蔔,且每天只吃一個,蘿蔔有四種顏色,分別為:紅蘿蔔,白蘿蔔,黃蘿蔔,發霉的蘿蔔(黑色),兔子吃了蘿蔔之後,體重會有不同的變化,紅蘿蔔吃了胖xg,白蘿蔔吃了胖yg,黃蘿蔔吃了瘦zg(醃黃蘿蔔真難吃...),兔子體重m,現在給你x,y,z,m問你幾天後,兔子的體重! 輸入:測資第一行是x,y,z,m,第二行是一串數字,1代表紅蘿蔔,2代表白蘿蔔,3代表黃蘿蔔,0代表沒吃。這一行中的數字為兔子這段時間內所吃的食物。 輸出:最後體重,若死掉及輸出-1 ``` ### 測資 輸入1: ``` 1 2 3 4 1 2 3 0 1 2 3 0 ``` 輸出1: ``` 4 ``` 輸入2: ``` 1 2 3 4 3 3 3 ``` 輸出2: ``` -1 ``` ### 範例程式 ```python= x,y,z,m = input().split() x = int(x) y = int(y) z = int(z) m = int(m) a = input().split() for i in range(len(a)): if int(a[i])==1: m+=x; if int(a[i])==2: m+=y; if int(a[i])==3: m-=z; if int(a[i])==0: pass if m < 0: print(-1) else: print(m) ``` ### 練習類題 https://zerojudge.tw/ShowProblem?problemid=a271 ## 題目7 區間和簡單版 ### 描述 ``` 輸入:一堆數字,跟兩個數字代表範圍 輸出:請求出這個範圍的數字相加,並輸出 ``` ### 測資 輸入1: ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 ``` 輸出1: ``` 3 ``` ### 範例程式 ```python= import math a = input().split() b,c = input().split() b = int(b) c = int(c) for i in range(len(a)): a[i]=int(a[i]) print(sum(a[b-1:c])) ``` ### 練習類題 https://zerojudge.tw/ShowProblem?problemid=e346 ### 普通補充 前綴和 ### 進階補充 線段數 LIS ## 題目7 費式列數 ### 費事數列定義 ``` https://zh.m.wikipedia.org/zh-tw/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0 ``` ### 描述 ``` 輸入:n 輸出:f(n) ``` ### 測資 輸入1: ``` 10 ``` 輸出1: ``` 55 ``` ### 範例程式 ```python= def f(n): if n==1 or n==2: return n return f(n-1)+f(n-2) ``` ### 練習類題 https://zerojudge.tw/ShowProblem?problemid=f013 ### 普通補充 陣列 ### 進階補充 dp ### 課外題目 https://zerojudge.tw/ShowProblem?problemid=b417 https://zerojudge.tw/ShowProblem?problemid=c290 https://zerojudge.tw/ShowProblem?problemid=c294 https://zerojudge.tw/ShowProblem?problemid=h026 https://zerojudge.tw/ShowProblem?problemid=a105 https://zerojudge.tw/ShowProblem?problemid=a291 https://zerojudge.tw/ShowProblem?problemid=a740