# Zerojudge參考解答-Python Zerojudge是個接觸程式設計後練習題目時必去的網站,也是目前台灣人數使用人數最多的Judge網站。 這裡是[ericleaf](https://zerojudge.tw/UserStatistic?id=158879)以Python撰寫的參考解答的集中地,持續更新中。 ~~沒有的就是還沒答出來。~~ ## 依出處分類 ### 基礎題庫 [a001](#a001) [a002](#a002) [a003](#a003) [a004](#a004) [a005](#a005) [a006](#a006) [a009](#a009) [a010](#a010) [a013](#a013) [a015](#a015) [a017](#a017) [a020](#a020) [a022](#a022) [a034](#a034) [a038](#a038) [a040](#a040) [a044](#a044) [a042](#a042) [a053](#a053) [a065](#a065) [a095](#a095) [a104](#a104) [a147](#a147) [a149](#a149) [a216](#a216) [a224](#a224) [a410](#a410) [a738](#a738) [d049](#d049) [d058](#d058) [d827](#d827) [e051](#e051) [f070](#f070) [f165](#f165) [g216](#g216) ### IOI/APCS [c294](#c294) [g275](#g275) ### UVa題庫 [a011](#a011) [a012](#a012) [a111](#a111) [a130](#a130) [a131](#a131) ### 原創/不分類題庫 ## 依題號排列 ### [a001](https://zerojudge.tw/ShowProblem?problemid=a001)<span id="a001"></span> ``` s=input() print("hello,",s) ``` ### [a002](https://zerojudge.tw/ShowProblem?problemid=a00)<span id="a002"></span> ``` a,b=map(int,input().split()) print(a+b) ``` ### [a003](https://zerojudge.tw/ShowProblem?problemid=a003)<span id="a003"></span> ``` m,d=map(int,input().split()) s=(m*2+d)%3 if s==0: print("普通") elif s==1: print("吉") else: print("大吉") ``` ### [a004](https://zerojudge.tw/ShowProblem?problemid=a004)<span id="a004"></span> 之前碰到這種輸入直到EOF結束的題目都是用陣列處理掉,後來發現還可以用try來解決。 ``` n = int(input('n=')) l = [] for i in range(n): l.append(int(input('year:'))) for i in l: if (i % 4 == 0 and i % 100 != 0) or i % 400 == 0: print('a leap year') else: print('a normal year') ``` ``` while 1: try: y=int(input()) if ((y%4==0) and (y%100!=0)) or (y%400==0): print("閏年") else: print("平年") except: break ``` ### [a005](https://zerojudge.tw/ShowProblem?problemid=a005)<span id="a005"></span> ``` a,b,c,d=map(int,input().split()) if b-a == d-c: print(a,b,c,d,2*d-c) else: print(a,b,c,d,int(d**2/c)) ``` ### [a006](https://zerojudge.tw/ShowProblem?problemid=a006)<span id="a006"></span> ``` a,b,c=map(int,input().split()) try: x1=int((((b**2-4*a*c)**0.5)-b)/(2*a)) x2=int((-((b**2-4*a*c)**0.5)-b)/(2*a)) if x1!=x2: print("Two different roots x1=",x1," , x2=",x2,sep="") else: print("Two same roots x=",x1,sep="") except: print("No real root") ``` ### [a009](https://zerojudge.tw/ShowProblem?problemid=a009)<span id="a009"></span> ``` s=input() for x in s: print(chr(ord(x)-7),end="") ``` ### [a010](https://zerojudge.tw/ShowProblem?problemid=a010)<span id="a010"></span> ``` n=int(input()) d=2 l=[] while n!=1: if n%d==0: n=n/d l.append(d) d=2 continue d+=1 x=0 while x<len(l): if l.count(l[x])>1: print(l[x],"^",l.count(l[x]),sep="",end="") x+=l.count(l[x]) else: print(l[x],end="") x+=1 if x!=len(l): print(" * ",end="") ``` ### [a011](https://zerojudge.tw/ShowProblem?problemid=a011)<span id="a011"></span> ``` n=1 while 1: try: s=input() s=" "+s n=0 for x in range(1,len(s)): if ((65<=ord(s[x])<=90) or (97<=ord(s[x])<=122)) and (not((65<=ord(s[x-1])<=90) or (97<=ord(s[x-1])<=122))): n+=1 print(n) except: break ``` ### [a012](https://zerojudge.tw/ShowProblem?problemid=a012)<span id="a012"></span> ``` while 1: try: n1, n2 = (int(i) for i in input().split()) print((n1-n2) if (n1>=n2) else (n2-n1)) except: break ``` ### [a013](https://zerojudge.tw/ShowProblem?problemid=a013)<span id="a013"></span> ``` def romanToInt(s): d = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000,'O':0} s = s + 'O' ans = 0 for i in range(len(s)-1): if d[s[i]] >= d[s[i+1]]: ans += d[s[i]] else: ans -= d[s[i]] return ans while 1: inp = input() if inp == '#': break n1 ,n2 = inp.split() if n1 == n2: print('ZERO') continue diff = romanToInt(n1) - romanToInt(n2) if diff < 0: diff *= -1 ans = '' intToRoman = {1000:'M',900:'CM',500:'D',400:'CD',100:'C',90:'XC',50:'L',40:'XL',10:'X',9:'IX',5:'V',4:'IV',1:'I'} for i in tuple(intToRoman.keys()): ans += intToRoman[i] * (diff // i) diff -= i * (diff // i) print(ans) ``` ### [a015](https://zerojudge.tw/ShowProblem?problemid=a015)<span id="a015"></span> ``` while 1: try: r,c=map(int,input().split()) l=[[0]*c]*r for x in range(r): l[x] = input().split() for x in range(c): for y in range(r): print(l[y][x],end=" ") print() except: break ``` ### [a017](https://zerojudge.tw/ShowProblem?problemid=a017)<span id="a017"></span> 究級偷懶寫法,因為全部的除法都是為整數除,所以要替換掉。 ``` while 1: try: ans = '' for i in input(): if i == '/': ans += '//' else: ans += i print(eval(ans)) except: break ``` ### [a020](https://zerojudge.tw/ShowProblem?problemid=a020)<span id="a020"></span> ``` d={"A":1,"B":10,"C":19,"D":28,"E":37,"F":46,"G":55,"H":64,"I":39,"J":73,"K":82,"L":2,"M":11,\ "N":20,"O":48,"P":29,"Q":38,"R":47,"S":56,"T":65,"U":74,"V":83,"W":21,"X":3,"Y":12,"Z":30} c=input() n=0 if (d[c[0]]+int(c[1])*8+int(c[2])*7+int(c[3])*6+int(c[4])*5+int(c[5])*4+int(c[6])*3+int(c[7])*2+int(c[8])+int(c[9]))%10==0: print("real") else: print("fake") ``` ### [a021](https://zerojudge.tw/ShowProblem?problemid=a021)<span id="a021"></span> 笑死。 雖然Python的寫法應該跟[a017](#a017)類似,反正沒有變數限制。 ``` print(eval(input().replace('/','//'))) ``` ### [a022](https://zerojudge.tw/ShowProblem?problemid=a022)<span id="a022"></span> ``` s=input() for x in range(len(s)): if s[x]==s[len(s)-1-x]: continue print("no") break else: print("yes") ``` ### [a034](https://zerojudge.tw/ShowProblem?problemid=a034)<span id="a034"></span> ``` while 1: try: n=int(input()) s="" while n>0: s=str(n%2)+s n=n//2 print(s) except: break ``` ### [a038](https://zerojudge.tw/ShowProblem?problemid=a038)<span id="a038"></span> ``` n=int(input()) if n==0: print(0) else: while n%10==0: n=n//10 for x in range(len(str(n))-1,-1,-1): print(str(n)[x],end="") ``` ### [a040](https://zerojudge.tw/ShowProblem?problemid=a040)<span id="a040"></span> ``` n,m=map(int,input().split()) t=0 for x in range(n,m+1): s=str(x) n=0 for y in s: n+=int(y)**(len(s)) if n == x: print(x,end=" ") t+=1 if t==0: print("none") ``` ### [a042](https://zerojudge.tw/ShowProblem?problemid=a042)<span id="a042"></span> ``` while 1: try: n = int(input()) area = 2 * n if n >= 3: area += ((n - 1) * (n - 2)) print(area) except: break ``` ### [a044](https://zerojudge.tw/ShowProblem?problemid=a044)<span id="a044"></span> ``` while 1: try: n = int(input()) print(n+1+n*n*(n-1)//2-(n-1)*n*(2*n-1)//6) except: break ``` ### [a053](https://zerojudge.tw/ShowProblem?problemid=a053)<span id="a053"></span> ``` n = int(input()) score = 0 if n <= 10: score = 6 * n elif n <= 20: score = 40 + 2 * n elif n <= 40: score = 60 + n else: score = 100 print(score) ``` ### [a065](https://zerojudge.tw/ShowProblem?problemid=a065)<span id="a065"></span> ``` while 1: try: s=input() for x in range(0,6): print(abs(ord(s[x])-ord(s[x+1])),end="") print() except: break ``` ### [a095](https://zerojudge.tw/ShowProblem?problemid=a095)<span id="a095"></span> ``` while 1: try: n,m=map(int,input().split()) if n==m: print(n) else: print(m+1) except: break ``` ### [a104](https://zerojudge.tw/ShowProblem?problemid=a104)<span id="a104"></span> ``` while 1: try: n=int(input()) l=list(map(int,input().split())) l.sort() for x in range(n-1): print(l[x],end=" ") print(l[n-1]) except: break ``` ### [a111](https://zerojudge.tw/ShowProblem?problemid=a111)<span id="a111"></span> ``` n=1 while 1: n=int(input()) if n==0: break s=0 for x in range(1,n+1): s+=x**2 print(s) ``` ### [a130](https://zerojudge.tw/ShowProblem?problemid=a130)<span id="a130"></span> ``` t = int(input()) for i in range(t): link = ['']*10 vi = [0]*10 for j in range(10): link[j], vi[j] = input().split() vi = [int(k) for k in vi] print('Case #%d:'%(i+1)) maxIndex = [] for l in range(10): if vi[l] == max(vi): maxIndex.append(l) for m in maxIndex: print(link[m]) ``` ### [a131](https://zerojudge.tw/ShowProblem?problemid=a131)<span id="a131"></span> ``` print(' '*9,'NAME',' '*21,'SOUNDEX CODE',sep='') while 1: try: name = input() soundexCode = name[0] code = {'B':'1','P':'1','F':'1','V':'1','C':'2','S':'2','K':'2','G':'2','J':'2','Q':'2','X':'2','Z':'2','D':'3','T':'3','L':'4','M':'5','N':'5','R':'6','A':'','E':'','I':'','O':'','U':'','Y':'','W':'','H':''} for i in range(1,len(name)): if code[name[i]] != code[name[i-1]]: soundexCode += code[name[i]] if len(soundexCode) == 4: break else: soundexCode += '0'*(4-len(soundexCode)) print(' '*9,'%-25s%s'%(name,soundexCode),sep='') except: print(' '*19,'END OF OUTPUT',sep='') break ``` ### [a147](https://zerojudge.tw/ShowProblem?problemid=a147)<span id="a147"></span> ``` while 1: n=int(input()) if n==0: break for x in range(1,n): if x%7!=0: print(x,end=" ") print() ``` ### [a149](https://zerojudge.tw/ShowProblem?problemid=a149)<span id="a149"></span> ``` t = int(input()) for i in range(t): n = input() ans = 1 for j in n: ans *= int(j) print(ans) ``` ### [a216](https://zerojudge.tw/ShowProblem?problemid=a216)<span id="a216"></span> 解題報告在[這裡](https://zerojudge.tw/ShowThread?postid=33398&reply=0) ``` def f(n): return int((1+n)*n/2) def g(n): return int(((1+n)*n/2)*(n+1)-n*(n+1)*(2*n+1)/6) while True: try: n = int(input()) print(f(n),g(n)) except: break ``` ### [a224](https://zerojudge.tw/ShowProblem?problemid=a224)<span id="a224"></span> ``` while 1: try: sen = input() word = {} for i in sen: if (ord(i) >= 97 and ord(i) <= 122): try: word[i] += 1 except: word[i] = 1 if (ord(i) >= 65 and ord(i) <= 90): try: word[chr(ord(i)+32)] += 1 except: word[chr(ord(i)+32)] = 1 odd = 0 for j in tuple(word.values()): odd += j % 2 if odd > 1: print('no...') break else: print('yes !') except: break ``` ### [a410](https://zerojudge.tw/ShowProblem?problemid=a410)<span id="a410"></span> 這題有用到以前學的克拉瑪公式。 ``` a,b,c,d,e,f=map(int,input().split()) if (a*e-b*d)!=0: print("x=","%4.2f"%((c*e-b*f)/(a*e-b*d)),"\n","y=","%4.2f"%((a*f-c*d)/(a*e-b*d)),sep="") elif (c*e-b*f)==0 and (a*f-c*d)==0: print("Too many") else: print("No answer") ``` ### [a738](https://zerojudge.tw/ShowProblem?problemid=a738)<span id="a738"></span> ``` while 1: try: a,b=map(int,input().split()) if a>b: a,b=b,a t=a while t!=0: t=b%a b=a a=t print(b) except: break ``` ### [c294](https://zerojudge.tw/ShowProblem?problemid=c294)<span id="c294"></span> ``` l=sorted(list(map(int,input().split()))) a=l[0] b=l[1] c=l[2] print(a,b,c) if (a+b>c) and (a+c>b) and (b+c>a): if (a*a+b*b<c*c): print("Obtuse") elif (a*a+b*b>c*c): print("Acute") else: print("Right") else: print("No") ``` ### [d049](https://zerojudge.tw/ShowProblem?problemid=d049)<span id="d049"></span> ``` print(int(input())-1911) ``` ### [d058](https://zerojudge.tw/ShowProblem?problemid=d058)<span id="d058"></span> ``` i = int(input()) if i > 0: print(1) elif i < 0: print(-1) else: print(0) ``` ### [d827](https://zerojudge.tw/ShowProblem?problemid=d827)<span id="d827"></span> ``` n = int(input()) print(n//12*50+n%12*5) ``` ### [e051](https://zerojudge.tw/ShowProblem?problemid=e051)<span id="e051"></span> ``` s=input() print(s[0],"_"*(len(s)-2),s[len(s)-1],sep="") ``` ### [f070](https://zerojudge.tw/ShowProblem?problemid=f070)<span id="f070"></span> ``` a1,b1=map(int,input().split()) a2,b2=map(int,input().split()) a3,b3=map(int,input().split()) n=1 while 1: if (n%a1==b1) and (n%a2==b2) and (n%a3==b3): break n+=1 print(n) ``` ### [f165](https://zerojudge.tw/ShowProblem?problemid=f165)<span id="f165"></span> ``` l,c=map(int,input().split()) if c==0: print("OK!") else: if l%c==0: print("OK!") else: print(l%c) ``` ### [g216](https://zerojudge.tw/ShowProblem?problemid=g216)<span id="g216"></span> ``` print("2^(10^%d)"%int(input())) ``` ### [g275](https://zerojudge.tw/ShowProblem?problemid=g275)<span id="g275"></span> ``` n=int(input()) for x in range(n): u=input().split() d=input().split() if ((u[1]==u[5]!=u[3]) and (d[1]==d[5]!=d[3])) and ((u[6]=="1") and (d[6]=="0")) and ((u[1]!=d[1]) and (u[3]!=d[3]) and (u[5]!=d[5])): print("None") else: if (u[1]==u[3]) or (u[1]!=u[5]) or (d[1]==d[3]) or (d[1]!=d[5]): print("A",end="") if (u[6]!="1") or (d[6]!="0"): print("B",end="") if (u[1]==d[1]) or (u[3]==d[3]) or (u[5]==d[5]): print("C",end="") print() ```