[TOC] --- ## (2023/11/25)- 迴圈, 陣列 ### [d244. 一堆石頭] https://zerojudge.tw/ShowProblem?problemid=d244 (Python) 思考方向 ``` 1.先把輸入排序 2.for迴圈計算有a[i]相同的有幾個 3.如果算出來為2就輸出+break迴圈 ``` ### [b030: 健康檢查(Ⅰ)] https://judge.tcirc.tw/ShowProblem?problemid=b030 ### [a693. 吞食天地] https://zerojudge.tw/ShowProblem?problemid=a693 (解題思路) https://hackmd.io/DwmkclCaTUOO48NYFW8AHw?view#a693-%E5%90%9E%E9%A3%9F%E5%A4%A9%E5%9C%B0 --- ## (2023/11/18)- 迴圈 ### [b021: 電電找因數] https://judge.tcirc.tw/ShowProblem?problemid=b021 (Python) 解題範例一 ```python= ## Python language ## solution link(含註解): https://github.com/CBJ0519/CBJsProgramDiary.com/blob/main/zj/a010.py n=int(input()) tmp=[] for i in range(2,int(1e9)): if n==1: break cnt=0 while not n%i: cnt+=1 n//=i if cnt: tmp.append([i,cnt]) ans=[] for i,cnt in tmp: if cnt>1: ans.append(f'{i}^{cnt}') else: ans.append(i) print(*ans,sep=' * ') ``` (Python) 解題範例二 ```python= ''' 20200717 jlhung v1.0 ''' #https://github.com/jlhung/ZeroJudge-Python/blob/master/a010-%20%E5%9B%A0%E6%95%B8%E5%88%86%E8%A7%A3.py while True: try: n = int(input()) x = 2 #因數 count = 0 #次方 ans = "" chk = 0 #檢查乘號 while True: #整除則繼續 if n % x == 0: count += 1 n //= x continue #不整除檢查次數後印出 或因數x大於n也印出 if n % x != 0 or x > n: if count == 0: x += 1 continue #檢查乘號 if chk != 0: ans = ans + " * " if count > 1: ans = ans + "{}^{}".format(x, count) elif count == 1: ans = ans + "{}".format(x) x += 1 count = 0 chk = 1 if x > n: break print(ans) except(EOFError): break ``` (C/C++語言) 解題範例一 ```c= #include <stdio.h> int main() { int input; scanf("%d", &input); for(int i=2;i<=input;i++) if(input%i==0) { int r=0, j=0; while(r==0) { input=input/i; r=input%i; j++; } if(j==1) printf("%d", i); else printf("%d^%d", i, j); if(input!=1) printf(" * "); } return 0; } ``` ### [b020: 慎思湖的倒影] https://judge.tcirc.tw/ShowProblem?problemid=b020 (C/C++語言) 解題範例一 ```c= #include <iostream> using namespace std; int main() { int l; cin >> l; for(int i=1;i<=l;i++){ for(int j=1;j<=i;j++){ cout << "*"; } cout << "\n"; } for(int i=1;i<=l;i++){ for(int j=l-i;j>=0;j--){ cout << "*"; } cout << "\n"; } } ``` --- ## (2023/11/11)- 關係運算子, 迴圈 ### [a017: BASIC 的 SGN 函數] https://judge.tcirc.tw/ShowProblem?problemid=a017 (Python) 解題範例一 ```python= # 觀念: True = 1, False = 0 # 正整數 傳回 1, (n > 0) -> True -> 1 # 負數 傳回 -1, (n < 0) -> True -> 1, 所以前面加個負號 ( -(n <0) ) -> -(1) -> -1 # 至於 n = 0, 傳回 0 就不用理它, 因為 (n > 0) = 0, -(n <0) = -0, 0 - 0 還是 0, 剛好符合 需求 # print( (n > 0) + (-(n < 0)) ) import sys for n in sys.stdin: n = int(n) print(int(n > 0) - int(n < 0)) ``` (C/C++語言) 解題範例一 ```c= #include <iostream> using namespace std; int main() { //給你一個整數 n, //若 n > 0 請輸出 1, //若 n = 0 請輸出 0, //若 n < 0 請輸出 -1。 int n; cin >> n; //if 判斷 if (n > 0) { cout << "1\n"; } else if (n == 0) { cout << "0\n"; } else { cout << "-1\n"; } return 0; } ``` (C/C++語言) 解題範例二 ```c= #include <iostream> using namespace std; int main() { //給你一個整數 n, //若 n > 0 請輸出 1, //若 n = 0 請輸出 0, //若 n < 0 請輸出 -1。 int n; cin >> n; //只用關係運算子及算術運算子 cout << (n > 0) - (n < 0) << "\n"; return 0; } ``` ### [a007: K-I-S-S-I-N-G] https://judge.tcirc.tw/ShowProblem?problemid=a007 (Python) 解題範例一 ```python= import sys for name in sys.stdin: name = name.split() print(name[0] + " and " + name[1] + " sitting in the tree") ``` ### [a030: 電腦教室] https://judge.tcirc.tw/ShowProblem?problemid=a030 (Python) 解題範例一 ```python= # 輸入正整數 n n = int(input()) # 輸入 n 個由空白隔開的正整數 numbers = input("".format(n)).split() # 將數字轉換為整數 numbers = list(map(int, numbers)) # 找出最大值 max_value = max(numbers) # 輸出最大值 print(max_value) ``` ### [a026: 文文的求婚--續集 (0 尾版)] https://judge.tcirc.tw/ShowProblem?problemid=a026 (Python) 解題範例一 ```python= try: while True: y=int(input()) print(("閏年" if ((y%4==0 and y%100!=0) or y%400==0) else "平年")) # print(("平年","閏年")[(y%4==0 and y%100!=0) or y%400==0]) except: pass ``` --- ## (2023/11/05)- 字元處理, 流程控制 ### [a003: 提款卡密碼] https://judge.tcirc.tw/ShowProblem?problemid=a003 (Python) 解題範例一 ```python= while True: try: text = list(input()) # 將輸入的文字轉換成串列 # 依序取出串列中每個字 ( 因為內容計算時會使用 i+1,所以此處使用 -1 ) for i in range(len(text)-1): print(f'{abs(ord(text[i+1]) - ord(text[i]))}',end='') # 計算出密碼 print() except: break ``` (Python) 解題範例二 ```python= ''' 20200724 v1.0 jlhung 轉成ASCII處理 ''' while True: try: n = input() pwd = "" for i in range(0, len(n)-1): pwd += str(abs(ord(n[i]) - ord(n[i+1]))) #取絕對值轉字串 print(pwd) except(EOFError): break ``` (C語言) 解題範例一 ```c= #include <stdio.h> #include <math.h> int main() { char password[8]; scanf("%s", password); for(int i=0;i<6;i++) printf("%d", abs(password[i]-password[i+1])); return 0; } ``` ### [a002: 電話客服中心] (迴圈, 一維陣列) https://judge.tcirc.tw/ShowProblem?problemid=a002 (Python) 解題範例一 ```python= ## Python language d={'A':'10','B':'11','C':'12','D':'13','E':'14',\ 'F':'15','G':'16','H':'17','I':'34','J':'18',\ 'K':'19','L':'20','M':'21','N':'22','O':'35',\ 'P':'23','Q':'24','R':'25','S':'26','T':'27',\ 'U':'28','V':'29','W':'32','X':'30','Y':'31',\ 'Z':'33'} s=input() def check(t): ans=int(t[0]) for i in range(1,10): ans+=int(t[i])*(10-i) x=(10-ans%10) if (10-ans%10)!=10 else 0 return (x == int(t[-1])) for i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": if check(d[i]+s): print(i, end = '') print() ``` (註- Ryan) 直接在 terminal 用 python3 編譯, 能成功執行且輸出正確. 但從 judge 系統輸入時, 則會得到 WA(line2) 的結果. 待查出問題原因. ![image](https://hackmd.io/_uploads/ryNlNTnXp.png) (Python) 解題範例二 ```python= while True: try: # 建立字母與數字對照表 table = {'A':10,'B':11,'C':12,'D':13,'E':14,'F':15,'G':16,'H':17,'I':34, 'J':18,'K':19,'L':20,'M':21,'N':22,'O':35,'P':23,'Q':24,'R':25, 'S':26,'T':27,'U':28,'V':29,'W':32,'X':30,'Y':31,'Z':33} n = [int(i) for i in list(input())] # 將輸入的文字轉換成數字串列 s = 0 # 建立變數 s 作為計算使用 for i in range(8): s = s + n[i]*(8-i) # 根據公式算出 s for i in table: # 依序取出對照表內所有的字母與數字 a = [int(j) for j in list(str(table[i]))] # 將對應的數字轉換成數字串列,例如 11 轉換成 [1, 1] if n[8] == 0: # 如果檢查碼為 0,套用對應檢查公式 if (s + a[0] + a[1]*9)%10 == 0: # 如果字母對應的數字符合公式,印出該字母 print(i, end='') else: if 10 - (s + a[0] + a[1]*9)%10 == n[8]: # 如果字母對應的數字符合公式,印出該字母 print(i, end='') print() except: break ``` (Python) 解題範例三 ```python= ''' 20200724 v1.0 jlhung ''' P = {"A":10, "B":11, "C":12, "D":13, "E":14, "F":15, "G":16, "H":17, "I":34, "J":18, "K":19, "L":20, "M":21, "N":22, "O":35, "P":23, "Q":24, "R":25, "S":26, "T":27, "U":28, "V":29, "W":32, "X":30, "Y":31, "Z":33 } while True: try: n = input() total = 0 #處理英文 轉ASCII處理 #total = P[n[0]] // 10 + P[n[0]] % 10 * 9 #處理中間8碼 c = 8 for t in n[0:8]: total += int(t) * c c -= 1 #處理尾碼 total += int(n[8]) ans = "" for keys, values in P.items(): if (P[keys] // 10 + P[keys] % 10 * 9 + total) % 10 == 0: ans = ans + keys print(ans) except(EOFError): break ``` (C語言) 解題範例一 ```c= #include <stdio.h> int main() { char num[10]; scanf("%s", num); int s=0; for(int i=0;i<8;i++) s+=(num[i]-48)*(8-i); int n; if(s%10<10-(num[8]-48)) n=(10-(num[8]-48))-s%10; else n=10+(10-(num[8]-48))-s%10; if(n==10) n=0; switch(n) { case 0: printf("BNZ"); break; case 1: printf("AMW"); break; case 2: printf("KLY"); break; case 3: printf("JVX"); break; case 4: printf("HU"); break; case 5: printf("GT"); break; case 6: printf("FS"); break; case 7: printf("ER"); break; case 8: printf("DOQ"); break; case 9: printf("CIP"); break; } return 0; } ``` --- ## 觀念題 (2023 Jun) https://andyli.tw/apcs-20230604/ 氣泡排序題很多 有define add(x,y) (x+y) 二分搜尋法 Stack Queue a的n次方 找最大公因數的方法 帕斯卡三角形 ## 觀念題 (2022 Oct) https://andyli.tw/apcs-202210/ 星號(\*)圖 (考很多) 從小到大排序 (考很多) LCS 位元運算 溢位 unsigned int 2^31 merge sort 雙指針 XOR交換 (前往筆記) 遞迴 ## 參考連結 ### Python 教學 (STEAM 教育學習網) https://steam.oxxostudio.tw/category/python/index.html ### 初學者也適用的ZeroJudge C語言詳解 : a系列 https://hackmd.io/@Ctb88VZXRI2uLZD1aafx9g/BJzhp16Di ### 黃惟Zerojudge練習題目(Python) https://hackmd.io/@10946009/Zerojudge