###### tags: `APCS檢定營隊` # Day2 Answer ## P17 ```python= p = 0 for i in range(1,101,1): p+=i print(p) ``` ## P17-1 ```python= p = 0 n = int(input()) for i in range(1,n+1,1): p+=i print(p) ``` ## P17-2 ```python= n = int(input()) q = int(input()) ans = 0 for i in range(1,q+1,n): ans+=i print(ans) ``` ## P17-ex1 ```python= n = int(input()) for i in range(0,n): print(' '*(n-i-1),'*'*(2*(i+1)-1),sep='') ``` ## P17-ex2 ```python= n = int(input()) m = int(input()) if m==1: for i in range(0,n): print(' '*(n-i-1),'*'*(2*(i+1)-1),sep='') if m==2: for i in range(0,n): print('*'*(2*(i+1)-1),sep='') if m==3: for i in range(n-1,-1,-1): print(' '*(n-i-1),'*'*(2*(i+1)-1),sep='') ``` ## P18 ```python= n = int(input()) m = int(input()) while n!=0 and m!=0: if n<m: m%=n else: n%=m if n == 0: print(m) else: print(n) ``` ## P18-1 ```python= while True: n = int(input()) if n==0: break m = int(input()) while n!=0 and m!=0: if n<m: m%=n else: n%=m if n == 0: print(m) else: print(n) ``` ### P19 ```python= def cal(n): if n%4!=0: print('Normal year') if n%4==0 and n%100!=0: print('leap year') if n%100==0 and n%400!=0: print('Normal year') while True: n = int(input())+1911 if n<1911: break cal(n) ``` ## P18-2 ```python= while True: n = int(input()) if n==-1: break m = int(input()) if n<=0 or m<=0: continue while n!=0 and m!=0: if n<m: m%=n else: n%=m if n == 0: print(m) else: print(n) ``` ## P19 ```python= def cal(n): if n%4!=0: print('Normal year') if n%4==0 and n%100!=0: print('leap year') if n%100==0 and n%400!=0: print('Normal year') while True: n = int(input())+1911 if n<1911: break cal(n) ``` ## P20 ```python= def power(n): if n==1: return n return n*power(n-1) def C(n,m): return power(n)/(power(n-m)*power(m)) print(C(int(input()),int(input()))) ``` ## P21 ```python= def gcd(a,b): if b==0: return a return gcd(b,a%b) def lcm(a,b): return (a*b)/gcd(a,b) a = int(input()) b = int(input()) c = int(input()) print(lcm(lcm(a,b),c)) ``` ## P22 ```python= a = int(input()) b = int(input()) c = int(input()) print(a,b,c,sep='hello',end='How Are You') ``` ## P25 ```python= a = [] a.append(31) a.append(17) a.sort() a.append(21) a.insert(1,10) a.pop(0) print(a) ``` ## P25-1 ```python= t = int(input()) list = [1,2,3,4,5,6,7,8,9,10] for i in range(t): c = input() d = c.split() for i in range(1,len(d)): d[i]=int(d[i]) if d[0] == "add": list.append(d[1]) if d[0] == "insert": list.insert(d[1],d[2]) if d[0] == "remove": list.pop(d[1]) if d[0] == "remove-range": for i in range(d[1],d[2]+1): list.pop(d[1]) if d[0] == "swap": c = list[d[2]] list[d[2]]=list[d[1]] list[d[1]]=c if d[0] == "print": print(list) ``` ## P26 ```python= a = input() b = input() c = input() d = [[a[0],a[1],a[2]],[b[0],b[1],b[2]],[c[0],c[1],c[2]]] print(d) for i in range(3): print(d[i]) ``` ## INA的井井井字遊戲 ```cpp= #include "testlib.h" #include <iostream> #include <cstdlib> /* 亂數相關函數 */ #include <ctime> /* 時間相關函數 */ using namespace std; bool judge(int * table) { int win[8][3] = {{0, 1, 2}, {0, 3, 6,}, {0, 4, 8}, {1, 4, 7}, {2, 5, 8}, {3, 4, 5}, {6, 7, 8}, {2, 4, 6}}; for(int i=0;i<8;i++) if (table[win[i][0]]==table[win[i][1]]&&table[win[i][1]]==table[win[i][2]] && table[win[i][0]]!=0) return true; return false; } void output(int *table) { for (int i = 0 ;i <3;i++,cout<<endl) for(int x =0 ; x<3;x++) cout<<table[i*3+x]<<' '; } int main(int argc, char* argv[]) { registerGen(argc,argv,0); int n; //n = rnd.next(3,100); //cout<<n<<endl; cin>>n; while(n--) { int table[9] = {0}; int round = 1; int turn = 0; set<int> lst; while (!judge(table) and round <= 9){ int a; do{ a = rnd.next(0,8); } while (lst.count(a)); lst.insert(a); //cout<<a<<endl; cin>>a; table[a] = turn % 2 + 1; //output(table); //cout<<endl; turn += 1; round++; } turn+=1; if(judge(table)) cout<<"player "<<turn % 2 + 1<<" win"<<endl; else cout<<"peace"<<endl; } } ``` ## 請問您今天要來點紙牌遊戲嗎? ```python= def main(): def input_card(player_id:int)->int: nonlocal player, used_card inin = input().split() for card in inin: point = card_point(card) type_ = card_type(card) if(point==-1): print("point error") return -1 if(type_==-1): print("type error") return -1 if(used_card[point][type_]): print("duplicated card") return -1 player[player_id].append((point,type_)) # used_card[point][type_] = True player[player_id].sort() return 1 def youre_fucked(p): return p[4] def card_point(card:str)->int: point_dic = {'A':14, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '10':10, 'J':11, 'Q':12, 'K':13} if(card[:-1] not in point_dic.keys()): print(card[:-1]) return -1 return point_dic[card[:-1]] def card_type(card:str)->int: type_dic = {'S':3, 'H':2, 'D':1, 'C':0} if(card[-1] not in type_dic.keys()): return -1 return type_dic[card[-1]] def pair(p): for i in range(4): if(p[i][0]==p[i+1][0]): return p[i+1] return False def two_pair(p): match_once = False score = [] for i in range(4): if(p[i][0]==p[i+1][0] and p[i]!=score): score = p[i+1] if(match_once): return score match_once = True return False def three_of_a_kind(p): for i in range(3): if(p[i][0]==p[i+1][0]==p[i+2][0]): return p[i+2] return False def straight(p): points = [card[0] for card in p] + [card[0]+13 for card in p[:4]] for i in range(len(points)-4): if(points[i]+4==points[i+1]+3==points[i+2]+2==points[i+3]+1==points[i+4]): return p[4] return False def flush(p): return p[4] if(len(set([card[1] for card in p]))==1) else False def full_horse(p): return three_of_a_kind(p) if(three_of_a_kind(p)!=False and pair(p)!=False) else False def four_of_a_kind(p): if(len(set([card[0] for card in p]))==2): return p[4] if(p[4][0]==p[3][0]) else p[3] return False def straight_flush(p): return p[4] if(flush(p)!=False and straight(p)!=False) else False def judge(p): funcs = [youre_fucked, pair, two_pair, two_pair, three_of_a_kind, straight, flush, full_horse, four_of_a_kind, straight_flush] for number in range(8,-1,-1): score = funcs[number](p) if(score!=False): return number,score # ----------------------------------------------- player_count = 2 player = [[] for i in range(player_count)] used_card = [[False for i in range(4)] for i in range(15)] for i in range(2): if(input_card(i))==-1: return win_player = -1 win_number = -1 win_score = [] for i in range(player_count): number,score = judge(player[i]) if(number>win_number): win_player = i win_number = number win_score = score elif(number==win_number): if(score[0]>win_score[0] or (score[0]==win_score[0] and score[1]>win_score[1])): win_player = i win_number = number win_score = score elif(score==win_score): raise "U FUCK" # print(win_player) # print(win_number) # print(win_score) p = {1:'chino',2:'coco'} print(p[win_player+1],'win') if(__name__=='__main__'): n = int(input()) for i in range(n): main() ```