본문 바로가기

python-algorithm1422

[백준] 2530 1 2 3 4 5 6 7 8 9 10 import sys a,b,c=map(int,sys.stdin.readline().split()) s=int(sys.stdin.readline()) c+=s%60 if c>59: c-=60; b+=1 b+=s//60 if b>59: a+=b//60; b%=60 a+=b//60 if a>23: a%=24 print("%d %d %d"%(a,b,c)) cs 어려운문젠느 아닌데... 미처 생각하지 못한 예외가 있어서 디지털 시계 생각해보시면 쉽게 구현가능합니다 2021. 2. 23.
[백준] 2163 1 2 a,b=map(int, input().split()) print(a*b-1) cs 마지막 한번은 안잘라줘도 되니까..ㅎ 2021. 2. 23.
[백준] 2161 1 2 3 4 5 6 7 8 9 10 11 12 import collections tmp=collections.deque() for i in range(1,int(input())+1): tmp.append(i) ans=[] while len(tmp)!=1: ans.append(tmp[0]) tmp.popleft() tmp.append(tmp[0]) tmp.popleft() ans.append(tmp[0]) print(*ans) cs 크게 어려운 부분은 없구요 자료구조(Data structure)를 deque로 이용해서 구현했습니다. 2021. 2. 23.
[백준] 2052 1 2 3 4 5 6 7 8 n=int(input()) s= "%.250f"% (2**(-n)) last=len(s) for i in range(last-1, 1, -1): if s[i]!='0': last=i break print(s[:last+1]) cs 음.. 이 문제는 엄청 쉽다고 생각했는데 자꾸 틀려서 문제가 뭘까 고민해보니 과학적 표기법과 관련돼있습니다. 소숫점 자리가 엄청 길어지면 python에서는 과학적 표기법 (Scientific notification)을 하게 돼서 문제의 출력 만큼 자리수를 다 출력하지 않습니다. 그 부분을 해결해 구현했습니다. 2021. 2. 23.
[백준] 1718 1 2 3 4 5 6 7 8 9 myPlain=input() key=input() b=len(key) for i in range(len(myPlain)): if myPlain[i]==' ': print(" ",end='') else: if ord(myPlain[i])-ord(key[i%b]) 2021. 2. 23.
[백준] 1676 1 2 s=int(input()) print(s//5+s//25+s//125) cs 처음에는 5로 나눈 몫만 생각하고 5의 제곱수를 미처 생각 못하고 구현했습니다. 2021. 2. 23.
[백준] 1673 1 2 3 4 5 6 7 8 9 10 import sys while True: tmp=sys.stdin.readline() if not (tmp) : break n,k=map(int, tmp.split()) s=n while n//k: s=s+n//k n=n//k+n%k print(s) cs 2021. 2. 23.
[백준] 1672 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import sys def haedok(an,an1): if an=='A' and an1=='A' : return 'A' if an == 'A' and an1 == 'G': return 'C' if an == 'A' and an1 == 'C': return 'A' if an == 'A' and an1 == 'T': return 'G' if an == 'G' and an1 == 'A': return 'C' if an == 'G' and an1 == 'G': return 'G' if an == 'G' and an1 == 'C': return 'T' if .. 2021. 2. 19.
[백준] 1592 1 2 3 4 5 6 7 8 9 n,m,l=map(int, input().split()) game=[0]*n cnt=0 start=0 while game[0]!=m: game[start]+=1 start=(start+l)%n cnt+=1 print(cnt-1) cs 반복문 이용하면 쉽습니다 특이사항으로는 line 9에서 cnt -1 하는 이유는 while 문이 예상했던것과 다르게 1번 더 반복되야 멈추기 때문입니다. 2021. 2. 19.
[백준] 1475 1 2 3 4 5 6 7 tmp=[0]*10 n=input() for i in n: if int(i)==6 or int(i)==9 : tmp[6]+=1 else: tmp[int(i)]+=1 tmp[6]= (tmp[6]+1)//2 print(max(tmp)) cs 2021. 2. 19.
[백준] 1373 1 2 import sys print(oct(int(sys.stdin.readline(),2))[2:]) cs 내장함수int로 타입캐스팅하고 인자로 2 를 주면 아 이녀석은 2진수구나 라고 인식을합니다 그리고 oct 함수는 정수를 8진수로 바꿔주는 내장함수입니다. 마지막에 [2:]의 의미는 oct, bin 과 같은 함수의 출력형태가 앞에 두글자가 진법을 뜻하는 문자가 나와서 0,1 자리에 해당하는 문자를 지우고 출력하기 위해 위와 같이 작성했습니다. 초기에는 단순히 입력받아서 while, for문을 통해 단순하게 손으로 계산하는 방식과 유사하게 알고리즘을 작성했으나 시간초과가 떠서 내장함수를 이용했고 입력을 sys.stdin.readline() 말고 input()으로 받으면 한줄로도 가능하겠네요 2021. 2. 19.
[백준] 1316 1 2 3 4 5 6 n=int(input()) ans=n for i in range(n): tmp=input() if list(tmp)!=sorted(tmp, key=tmp.find): ans-=1 print(ans) Colored by Color Scripter cs 내장함수 sorted()를 통해서 그 알파벳 이 처음 나오는 것을 key로 삼아서 구현했습니다. 2021. 2. 19.