본문 바로가기

python-algorithm1422

[백준] 1264 1 2 3 4 5 6 7 8 moum='aeiou' while True: s = input().lower() if s=='#': exit() cnt=0 for i in s: if i in moum : cnt+=1 print(cnt) cs 문자열 중에 대문자가 있으면 인식을 못하므로 python 내장 함수 lower()을 이용해서 문자열 전체를 소문자화 시킵니다. 2021. 1. 6.
[백준] 1233 1 2 3 4 5 6 7 8 9 10 import sys s1,s2,s3=map(int,sys.stdin.readline().split()) tmp=[0]*81 for i in range(1,s1+1): for j in range(1, s2 + 1): for k in range(1, s3 + 1): tmp[i+j+k]+=1 for i in range(1,81): if tmp[i]==max(tmp): print(i); break cs 3중 for 문 써도 시간은 무난한데 메모리가 좀.. 2021. 1. 5.
[백준] 1225 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import sys a,b = sys.stdin.readline().split() tmpA=[] tmpB=[] tmp=0 for i in range(len(a)): tmpA.append( int (a[i])) for i in range(len(b)): tmpB.append( int (b[i])) for i in range(len(tmpA)): for j in range(len(tmpB)): tmp+= tmpA[i]*tmpB[j] print(tmp) cs 우선 pypy3로 풀었구요 최대 경우의수가 9999*9999 이므로 약 1억이거든요 시간이나 메모리 관리에 신경써야 할 문제 입니다. 2021. 1. 5.
[백준] 20053 1 2 3 4 5 import sys for _ in range(int(sys.stdin.readline())): dummy=sys.stdin.readline() tmp=list(map(int,sys.stdin.readline().split())) print(min(tmp),max(tmp)) cs python 내장함수 min, max를 이용해서 쉽게 풀었으나 정수의 개수가 백만 개에서 천만 개로 개수가 늘어난다면 조금 다른방법을 생각봐야지 않을까.. 싶습니다. 2021. 1. 5.
[백준] 19572 1 2 3 4 5 6 7 8 import sys d1,d2,d3=map(int,sys.stdin.readline().split()) if (d1+d2-d3)/2 2021. 1. 5.
[백준] 18247 1 2 3 4 5 import sys for _ in range(int(sys.stdin.readline())): n, m = map(int, sys.stdin.readline().split()) if n 2021. 1. 5.
[백준] 17945 1 2 3 4 5 6 import sys import math a,b=map(int, sys.stdin.readline().split()) if -a+int (math.sqrt(a**2-b))== -a-int (math.sqrt(a**2-b)): print(-a+int (math.sqrt(a**2-b))) else: print (-a-int (math.sqrt(a**2-b)),-a+int (math.sqrt(a**2-b))) cs 문제 설명은 길지만 근의공식을 구현하는 방식입니다. 2021. 1. 5.
[백준] 17618 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import sys def singi(n): tmp=n cnt=0 while tmp!=0: cnt+=tmp%10 tmp=tmp//10 if n%cnt==0 : return 1 else : return 0 n=int(sys.stdin.readline()) ans=0 for i in range(1,n+1): if singi(i)==1: ans+=1 print (ans) cs 일단 이 문제도 저처럼 하시면 python3 로는 시간초과뜹니다. 구현 방식이 얼추 비슷한것같은데 고민이 좀 더 필요해보이네요 2021. 1. 5.
[백준] 17450 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import sys s_price,s_weight=map(int,sys.stdin.readline().split()) n_price,n_weight=map(int,sys.stdin.readline().split()) u_price,u_weight=map(int,sys.stdin.readline().split()) if s_price*10>=5000: s_gsb= (s_weight*10)/(s_price*10 - 500) else : s_gsb=(s_weight*10)/(s_price*10 ) if n_price*10>=5000: n_gsb= (n_weight*10)/(n_price*10 - 500) else : n_gsb=(n_weight*10).. 2021. 1. 5.
[백준] 17356 1 2 3 4 5 import sys a,b=map(int,sys.stdin.readline().split()) m=(b-a)/400 print(1/(1+pow(10,m))) cs 힌트에 처럼 python 에서는 pow 함수를 이용하시면 됩니당 2021. 1. 5.
[백준] 17284 1 2 3 4 5 6 7 8 import sys cnt=5000 tmp=list(map(int, sys.stdin.readline().split())) for i in range(len(tmp)): if tmp[i]==1: cnt-=500 elif tmp[i] == 2: cnt -= 800 elif tmp[i] == 3: cnt -= 1000 print(cnt) Colored by Color Scripter cs 문제명 Vending machine , 한국어로는 자판기죠 자판기 기능 생각해보시면서 구현하시면 쉽습니다. 예외조건으로 주어진 돈을 초과해서 버튼을 누르거나 잔돈의 종류별 갯수를 출력해야하는데 특정 잔돈이 부족하다거나 하는 조건이 있으면 더 재밌겠네요 2021. 1. 5.
[백준] 17174 1 2 3 4 5 6 7 8 import sys a,b = map(int, sys.stdin.readline().split()) cnt=a while True: cnt+=a//b a= a//b if a==0: break print(cnt) Colored by Color Scripter cs 제가 설정한 변수 a가 문제에서는 N에 해당하니 1달러, 10달러, 100달러 .. 과 같이 지폐를 세는방식 line3 에서 초기값을 설정해줍니다 2021. 1. 5.