--- title: 2020 pre _ A tags: ytp --- :::info ::: ## 2020 初賽 ### 第一題 #### C++ ```cpp= #include <iostream> using namespace std; int main(){ int K; int count; cin >> K; count = K / 7; if(count == 0){ cout << 5; return 0; } if(K % 7 == 0 && count != 0){ cout << count * 5; return 0; } else { cout << (count+1) * 5; return 0; } } ``` #### Python ```python= i = 0 while i < 1: mask = eval(input('enter a K: ')) try: mask = int(mask) except: print('格式錯誤,請輸入數字') continue if mask > 99 or mask <= 0: print('error') else: break a = 1 if mask % 7 == 0: ans = mask // 7 else: ans = mask // 7 + a num = ans * 5 print(num) ``` -------------------------------------------------------- ### 第二題 #### C++ ```cpp= #include <iostream> #include <cstring> #include <algorithm> #include <math.h> using namespace std; const std::string WHITESPACE = " \n\r\t\f\v"; std::string ltrim(const std::string &s){ size_t start = s.find_first_not_of(WHITESPACE); return (start == std::string::npos) ? "" : s.substr(start); } std::string rtrim(const std::string &s){ size_t end = s.find_last_not_of(WHITESPACE); return (end == std::string::npos) ? "" : s.substr(0, end + 1); } std::string trim(const std::string &s){ return rtrim(ltrim(s)); } int main(){ string text; string delimiter = ","; string nums[256]; getline(cin, text); //比cin還強,可將空格一起輸進來 int count = 0; string num; size_t pos = 0; while ((pos = text.find(delimiter)) != string::npos){ num = text.substr(0, pos); nums[count++] = trim(num); text.erase(0, pos + delimiter.length()); } int array[256]; string y; nums[count] = trim(text); for(int i = 0; i <= count; i++){ y = nums[i]; array[i] = atoi(y.c_str());//將string轉為int } int a = array[1]; int b = array[2]; int c = array[3]; if(array[1] == 0){ if(array[0] == 1){ a = pow(c,2) - pow(b,2);//平方 a = sqrt(a);//開根號 cout << a << endl; } if(array[0] == 2){ a = c/2 + b; cout << a << endl; } if(array[0] == 3){ a = c / b; cout << a << endl; } } if(array[2] == 0){ if(array[0] == 1){ b = pow(c,2) - pow(a,2); b = sqrt(b); cout << b << endl; } if(array[0] == 2){ b = a - (c/2); cout << b << endl; } if(array[0] == 3){ b = c / a; cout << b << endl; } } if(array[3] == 0){ if(array[0] == 1){ c = pow(a,2) + pow(b,2); c = sqrt(c); cout << c << endl; } if(array[0] == 2){ c = (a - b) * 2; cout << c << endl; } if(array[0] == 3){ c = a * b; cout << c << endl; } } return 0; } ``` #### Python ```python= def one(a,b,c): ''' a ** 2 + b ** 2 = c ** 2 ''' if '' is a: b = int(b) c = int(c) a = (c ** 2 - b ** 2) ** 0.5 return '%d'%(a) if '' is b: a = int(a) c = int(c) b = (c ** 2 - a ** 2) ** 0.5 return '%d'%(b) if '' is c: a = int(a) b = int(b) c = (a ** 2 + b ** 2) ** 0.5 return '%d'%(c) else: print('wrong') def two(a,b,c): ''' (a - b) * 2 = c ''' if '' is a: b = int(b) c = int(c) a = (c / 2) + b return '%d'%(a) if '' is b: a = int(a) c = int(c) b = a - (c / 2) return '%d'%(b) if '' is c: a = int(a) b = int(b) c = (a - b) * 2 return '%d'%(c) def three(a,b,c): ''' a * b = c ''' if '' is a: b = int(b) c = int(c) a = c / b return '%d'%(a) if '' is b: a = int(a) c = int(c) b = c / a return '%d'%(b) if '' is c: a = int(a) b = int(b) c = a * b return '%d'%(c) lst = input('enter:').split(',') p = int(lst[0]) a = lst[1] b = lst[2] c = lst[3] print(lst) if p == 1: print(one(a,b,c)) elif p == 2: print(two(a,b,c)) elif p == 3: print(three(a,b,c)) else: print('wrong') ``` -------------------------------------------------------- ### 第三題 #### C++ ```cpp= #include <iostream> using namespace std; int main(){ int R, C; int count = 0; cin >> R >> C; string word; string temp; for(int i = 0; i <= R; i++){ getline(cin, word); for(int j = 0; j < C; j++){ if(word[j] == 'X'){ count++; } if(i > 0){ if(word[j] == 'X' && temp[j] == 'X'){ count--; } } } temp = word; } cout << count; return 0; } ``` #### Python ```python= import random click = 0 i = 0 test = [] while i < 1: lst = input('enter R & C:').split(' ') R = int(lst[0]) C = int(lst[1]) if 3 <= R <= 1000 and 1 <= C <= 8: break if R < 3 or R >1000: print('R should be 3~1000') if C < 1 or C > 8: print('C should be 1~8') elif C < 1 or C > 8: print('C should be 1~8') while i < R: test.append(list(input(''))) i += 1 for i in range(R): for j in range(C): if 'X' in test[i][j]: click += 1 if i == 0: pass elif test[i][j] == test[i - 1][j]: click -= 1 print(click) ``` -------------------------------------------------------- ### 第四題 #### C++ ```cpp= #include <iostream> using namespace std; int main(){ int N, S, C; double count; int stop = 0; int total = 0; int schoolstop = 0; cin >> N; for(int i = 0; i < N; i++){ cin >> S; int p = 0; for(int j = 0; j < S; j++){ cin >> C; int num = 0; int array[C]={}; for(int x = 0; x < C; x++){ cin >> array[x]; if(array[x] == 11){ num++; p++; } } if(num >= 1){ stop = stop + C; } if(num > 1 || p > 1){ schoolstop++; } total = total + C; } } count = N / 3.0; if(schoolstop >= count){ cout << total; } else { cout << stop; } return 0; } ``` #### Python ```python= ``` -------------------------------------------------------- ### 第五題 #### C++ ```cpp= #include <iostream> using namespace std; int main(){ int N; int K = 1; int temp; int count = 0; int judge = 0; cin >> N; int X = N/2; int array[N] = {}; for(int i = 0; i < N; i++){ cin >> array[i]; } if(judge == 0){ for(int i = 0; i < N; i++){ if(array[i] == i+1 || array[i] == array[i+1]){ cout << "-1"; return 0; } } } judge++; for(int i = 0; i < X; i++){ int Y = 0; while(array[i] != i+1){ for(int j = 0; j < N; j++){ if(array[i] == j+1){ temp = array[i]; array[i] = array[j]; array[j] = temp; } } } count++; for(int z = 0; z < N; z++){ if(array[z] == z+1){ Y++; } } if(Y == N){ break; } } for(int i = 0; i <= X; i++){ if(count == i+2){ K = K + i+1; } } cout << K; return 0; } ``` #### Python ```python= def wheter(): lst.sort() for j in range(people): if lst[j] == lst[j - 1]: print('-1') return False break if '0' in lst: print('-1') return False for j in range(people): if array[j] == j + 1: print('-1') return False break return True def cal(): circle = 1 count = 0 for k in range(len(array) // 2): y = 0 while array[k] != (k + 1): for q in range(people): if array[k] == (q + 1): array[k],array[q] = array[q],array[k] count += 1 for z in range(people): if array[z] == (z + 1): y += 1 if (y == people): break for k in range(len(array) // 2): if (count == (k + 2)): circle += (k + 1) print(circle) i= 0 while i < 1: people = int(input('ppl:')) if people < 1 or people > 5 * (10 ** 5): print('1 ≤ 𝑁 ≤ 5 ∙1 0^5') else: break while i < 1: lst = input('').split(' ') array = list(map(int,lst)) if len(array) != people: print('lst can only', people,'in list') continue else: break a = False a = wheter() if a is True: cal() ``` --------------------------------------------------------