본문 바로가기

python-algorithm1422

백준 2485 가로수 1 2 3 4 5 6 7 8 9 10 11 12 13 14 def gcd(a,b): if b==0 : return a else :return gcd(b,a%b) n=int(input()) nums=[int(input()) for i in range(n)] nums.sort() start=min(nums) end=max(nums) gcdAnswer=gcd(nums[1]-nums[0],nums[2]-nums[1]) for i in range(n-2): gcdAnswer = gcd(gcd(nums[i+1] - nums[i], nums[i+2] - nums[i+1]), gcdAnswer) print( (end-start)//gcdAnswer-n+1) Colored by Color Scripter cs 문제 읽어.. 2021. 12. 27.
백준 5347 LCM 1 2 3 4 5 6 7 8 def gcd(a,b): if b==0 : return a else :return gcd(b,a%b) n=int(input()) for _ in range(n): a,b=map(int,input().split()) print(gcd(a,b)*a//gcd(a,b)*b//gcd(a,b)) cs 최소 공배수는 최대 공약수를 알면 구할수있죵 2021. 12. 27.
백준 1940 주몽 1 2 3 4 5 6 7 8 9 import sys n=int(input()) m=int(input()) ingredientList=list(map(int, sys.stdin.readline().split())) ans=0 for i in range(n): for j in range(i+1,n): if ingredientList[i]+ingredientList[j]==m: ans+=1 print(ans) Colored by Color Scripter cs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n=int(input()) m=int(input()) ingredientList=list(map(int, input().split())) ingredientList.sort() start,.. 2021. 12. 27.
백준 14490 백대열 1 2 3 4 5 6 7 8 def gcd(a,b): if(b==0): return a else: return gcd(b,a%b) n,m=map(int,input().split(':')) returnGcd=gcd(n,m) print("%d:%d"%(n//returnGcd,m//returnGcd)) cs 2021. 12. 27.
백준 2670 연속부분최대곱 1 2 3 4 5 6 n=int(input()) realNumbers=[float(input()) for i in range(n)] dp=[realNumbers[0]] for i in range(n-1): dp.append(max(dp[i]*realNumbers[i+1],realNumbers[i+1])) print("%.3f"%(max(dp))) Colored by Color Scripter cs DP 에서 kadane 알고리즘 참고해서 점화식 구하고 구현 2021. 12. 23.
백준 2776 암기왕 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 def binary_search(target, data): # data.sort() start = 0 end = len(data) - 1 flag=0 while start 2021. 12. 23.
백준 17219 비밀번호 찾기 1 2 3 4 5 6 7 8 siteDB={} n,m=map(int, input().split()) for i in range(n): sitename,sitepw=input().split() siteDB[sitename]=sitepw targetSite=[input() for i in range(m)] for i in targetSite: print(siteDB[i]) cs 파이썬에서는 딕셔너리 {} 를 쓰면 편하죵 2021. 12. 23.
백준 10826 피보나치 수 4 1 2 3 4 5 6 7 8 from functools import lru_cache @lru_cache() def nth_fibo(n): if n==0: return 0 elif n==1 or n==2 : return 1 else : return nth_fibo(n-1)+nth_fibo(n-2) print(nth_fibo(int(input()))) cs 1 2 3 4 5 6 7 8 9 from functools import lru_cache import sys sys.setrecursionlimit(10**6) @lru_cache() def nth_fibo(n): if n==0: return 0 elif n==1 or n==2 : return 1 else : return nth_fibo(n-1)+nth.. 2021. 12. 22.
백준 1302 베스트셀러 1 2 3 4 5 6 7 8 from collections import Counter n=int(input()) tmp=[] for i in range(n): tmp.append(input()) tmp.sort() counter_tmp=Counter(tmp) print(counter_tmp.most_common(n=1)[0][0]) cs Line 6 처럼 정렬을 안해주면 문제에서 원하는 사전순으로 제일 앞선 값이 아닌 결과가 출력 되는 경우도 있습니다 2021. 12. 22.
백준 10825 국영수 1 2 3 4 5 6 7 8 9 10 11 n=int(input()) ans=[] for i in range(n): name,kor,eng,mathScore=input().split() kor=int(kor) eng = int(eng) mathScore = int(mathScore) ans.append([name,kor,eng,mathScore]) ans=sorted(ans, key=lambda x: (-x[1],x[2],-x[3],x[0]) ) for i in range(n): print(ans[i][0]) Colored by Color Scripter cs 2021. 12. 21.
백준 11656 접미사 배열 1 2 3 4 5 6 7 s=input() lengthS=len(s) ans=[] for i in range(lengthS): ans.append(s[i:]) for i in sorted(ans): print (i) cs 2021. 12. 21.
백준 1543 문서 검색 1 2 3 4 s=input() target=input() cnt=len(s) print(s.count(target)) cs python count는 중복 제거해서 알아서 카운팅해주네요 2021. 12. 21.