# Zerojudge 程式練習 ## [a009: 解碼器](https://zerojudge.tw/ShowProblem?problemid=a009) ### 參考程式 ```cpp= #include <stdio.h> int main() { char c; int d = 'J'-'C'; while(scanf("%c",&c)!=EOF) { printf("%c",c-d); } printf("\n"); return 0; } ``` ## [a022: 迴文](https://zerojudge.tw/ShowProblem?problemid=a022) ### cstring字串基本特性測試程式 ```cpp= #include <stdio.h> int main() { char s[1000]; int len=0,i; printf("輸入字串:"); scanf("%s",s); printf("輸入字串:%s\n",s); i=0; while(s[i++]!=0) len++; printf("字串長度:%d\n",len); return 0; } ``` ### 參考程式 ```cpp= #include <stdio.h> int main() { char s[1000]; int len=0,i,rt=1; scanf("%s",s); // 輸入字串,陣列變數前不用加& while(s[i++]!=0) len++; for(i=0; i<len; i++) if(s[i]!=s[len-i-1]) rt=0; //須能推出字串從後開始的索引 if(rt==1) printf("yes\n"); else printf("no\n"); return 0; } ``` ```cpp= // string version #include <iostream> #include <string> using namespace std; int main() { string s; int len,rt=1; cin>>s; // 輸入 len = s.length(); for(int i=0; i<len; i++) if(s[i]!=s[len-i-1]) rt = 0; if(rt==1) cout<<"yes\n"; else cout<<"no\n"; return 0; } ``` ## [a020: 身分證檢驗](https://zerojudge.tw/ShowProblem?problemid=a020) ### 參考程式 ```cpp= #include <stdio.h> int main() { //宣告 int idn[] = {10,11,12,13,14,15,16,17,34,18,19,20,21, 22,35,23,24,25,26,27,28,29,32,30,31,33}; char id[11]; int i,sum,t; //輸入 scanf("%s",id); //處理(ex: A123456789) // 1 查表 t = idn[id[0]-'A']; // 2 個位數*9+十位數 sum = (t%10)*9+t/10; // 3 取出數字再做數字加權 for(i=1; i<9; i++) sum+= (id[i]-'0')*(9-i); // 4 sum+= (id[9]-'0'); // 5 判斷 & 輸出 if(sum%10==0) printf("real\n"); else printf("fake\n"); return 0; } ``` ```python= # 建表 idn = 10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29,32,30,31,33 # 輸入 s = input() # 處理(ex: A123456789) # 1 查表 n = idn[ord(s[0])-ord('A')] # 2 個位數*9+十位數 sum = (n%10)*9+n//10 # 3 取出數字再做數字加權 for i in range(1,9): sum= sum+int(s[i])*(9-i) # 4 加上最後一碼 sum = sum+int(s[9]) # 5 判斷 & 輸出 if sum%10==0: print('real') else: print('fake') ``` ## [a010: 因數分解](https://zerojudge.tw/ShowProblem?problemid=a010) ### 參考程式 ```cpp= ``` ## [a040: 阿姆斯壯數](https://zerojudge.tw/ShowProblem?problemid=a040) ### 參考程式 ```cpp= ``` ## [c290. APCS 2017-0304-1秘密差](https://zerojudge.tw/ShowProblem?problemid=c290) ```python= # 輸入一個整數 n = input() # 處理 # 設定奇數位和、偶數位和初值為0 # 判斷奇偶位數,加到對應奇偶位數和 odds,evens = 0,0 for i in range(len(n)): if i%2==0: evens = evens+int(n[i]) else: odds = odds+int(n[i]) # 相減取絕對值後輸出 print(abs(evens-odds)) ```