본문 바로가기

python-algorithm1422

[백준] 1919 1 2 3 4 5 6 7 8 9 10 11 12 13 import sys s1=sys.stdin.readline() s2=sys.stdin.readline() tmp=[0]*26 tmp1=[0]*26 cnt=0 for i in range(len (s1)-1): tmp[ ord (s1[i]) -97 ] +=1 for i in range(len (s2)-1): tmp1[ ord (s2[i]) -97 ] +=1 for i in range(26): cnt+=abs(tmp[i]-tmp1[i]) print(cnt) cs 처음엔 순서가 중요한줄 알았는데 사실 순서는 별 의미가없고 알파벳별로 갯수만 체크하시면 됩니더 2021. 1. 6.
[백준] 1871 1 2 3 4 5 6 7 import sys for _ in range(int(sys.stdin.readline())): l,d=sys.stdin.readline().split('-') cnt= (ord (l[0])-65)*(26**2) + (ord(l[1])-65)*26+ord (l[2])-65 d= int(d) if abs(cnt-d)>100: print("not nice") else: print("nice") Colored by Color Scripter cs 2021. 1. 6.
[백준] 1813 1 2 3 4 5 6 7 8 9 10 11 12 import sys n=int(sys.stdin.readline()) hs=list(map(int,sys.stdin.readline().split())) tmp=[0]*100001 cnt=0 checker=0 for i in range(n): tmp[hs[i]]+=1 for i in range(len(tmp)): if tmp[i]==i: cnt=i; checker=1 if checker==0 : print(-1); exit() else: print(cnt) cs 말장난 같다고 할까요? 구현 능력보다는 명제를 찬찬히 읽어보시고 구현 하는 방식에 대해서 고민하시면 쉽게 풀 수 있습니다. 2021. 1. 6.
[백준] 1773 1 2 3 4 5 6 7 8 import sys n,c=map(int, sys.stdin.readline().split()) firework=[0]*(c+1) for _ in range(n): pok=int(sys.stdin.readline()) for i in range(c+1): if i%pok==0: firework[i]=1 print(sum(firework)-1) cs 시간초과나서 pypy 3로 돌렸습니다. 죄송합니다 시간줄여서 해볼게요 2021. 1. 6.
[백준] 1731 1 2 3 4 5 6 7 import sys n=int(sys.stdin.readline()) tmp=[] for _ in range(n): tmp.append(int(sys.stdin.readline())) if (tmp[1]-tmp[0])== (tmp[2]-tmp[1]): print( tmp[-1]+tmp[1]-tmp[0]) elif (tmp[1]%tmp[0])== (tmp[2]%tmp[1]): print( tmp[-1]*tmp[1]//tmp[0]) cs 어릴 때 배우셨던 등차, 등비 수열 조건? 점화식? 생각해보시면서 구현하시면 쉽습니다. 2021. 1. 6.
[백준] 1681 1 2 3 4 5 6 7 8 9 10 11 import sys a,b=sys.stdin.readline().split() a=int(a) cnt=0 n=0 while cnt!=a: n+=1 if b in str(n): continue cnt+=1 print(n) cs 본문 밑에 있는 힌트를 보시면 쉽고 python은 문자열 다루는게 다른 언어에 비해 쉬운편이라 쉽게 풀 수 있습니다. 2021. 1. 6.
[백준] 1668 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import sys n=int(sys.stdin.readline()) tmp=[] for _ in range(n): tmp.append(int(sys.stdin.readline())) leftmax=tmp[0] rightmax=tmp[len(tmp)-1] cntLeft=1 cntRight=1 for i in range(n): if tmp[i]>leftmax : leftmax=tmp[i]; cntLeft+=1 for i in range(n): if tmp[n-1-i]>rightmax : rightmax=tmp[n-1-i]; cntRight+=1 print(cntLeft) print(cntRight) Colored by Color Scripte.. 2021. 1. 6.
[백준] 1568 1 2 3 4 5 6 7 8 9 10 11 12 13 import sys n=int(sys.stdin.readline()) tmp=0 for _ in range(99999): if n 0: if n-cnt 2021. 1. 6.
[백준] 1551 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import sys def solution(A): tmp=[] for i in range(len(A)-1): tmp.append(A[i+1]-A[i]) A=tmp return A n,t = map(int, sys.stdin.readline().split()) A=list(map(int, sys.stdin.readline().split(","))) for _ in range(t): A=solution(A) for i in range(len(A)): if i!=len(A)-1:print(A[i],end=',') else: print(A[i]) cs solution 에 해당하는 우선 i+1항에서 i항을 뺀 것이 새로운 수열이고 그리고 다른 문제들.. 2021. 1. 6.
[백준] 1434 1 2 3 4 5 import sys n,m = map(int, sys.stdin.readline().split()) A=list(map(int,sys.stdin.readline().split())) B=list(map(int,sys.stdin.readline().split())) print(sum(A)-sum(B)) cs 문제가 길긴 한데 손으로 풀어보시면서 하면 간단하게 풀립니다. 브론즈2 난이도가 조금 아깝습니다. 2021. 1. 6.
[백준] 1392 1 2 3 4 5 6 7 8 9 10 11 import sys n,q = map(int,sys.stdin.readline().split()) tmp=[0] cnt=0 for i in range(n): cnt+= int(sys.stdin.readline()) tmp.append(cnt) for i in range(q): test=int(sys.stdin.readline()) for _ in range(len(tmp)): if tmp[_] 2021. 1. 6.
[백준] 1350 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import sys n=int(sys.stdin.readline()) if n>1: tmp=list(map(int, sys.stdin.readline().split())) cluster=int(sys.stdin.readline()) cnt=0 for i in range(n): if tmp[i]!=0: if tmp[i]%cluster!=0: cnt+=(tmp[i]//cluster) +1 else: cnt+= tmp[i]//cluster print(cnt*cluster) else: tmp=int(sys.stdin.readline()) cluster=int(sys.stdin.readline()) if tmp%cluster==0: pri.. 2021. 1. 6.