본문 바로가기

백준1056

백준 2828 사과 담기 게임 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import sys n,m= map(int, sys.stdin.readline().split()) j=int(sys.stdin.readline()) left=1 ans=0 for i in range(j): apple=int(sys.stdin.readline()) if apple left+m -1: ans+= apple-(left+m-1) left=apple-m+1 print(ans) Colored by Color Scripter cs 이게 백준문제는 문제 해석이 조금 어려워서 바구니의 왼쪽 끝을 기준으로 잡으면 되고 첫번째 예제의 첫번째 케이스를 곰곰히 생각해보면 바구니의 사이즈가 1이고 왼쪽 1칸만 차지하고 있으니 2번째 칸에 떨어지는 사과는 .. 2021. 8. 25.
백준 2108 통계학 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import sys from collections import Counter tmp=[] n=int(sys.stdin.readline()) for i in range(n): tmp.append(int(sys.stdin.readline())) tmp.sort() print( int (round (sum(tmp)/n,0))) print(tmp[n//2]) f = Counter(tmp) b = f.most_common() if len(tmp) > 1: if b[0][1] == b[1][1]: print(b[1][0]) else: print(b[0][0]) else: print(tmp[0]) print(tmp[.. 2021. 8. 24.
백준 10773 제로 1 2 3 4 5 6 7 8 import sys tmp=[] n=int(sys.stdin.readline()) for i in range(n): tmpNum=int(sys.stdin.readline()) if tmpNum==0: tmp.pop() else: tmp.append(tmpNum) print(sum(tmp)) cs 문제 보면 딱 드는 생각이 스택 떠오르시면 쉽게 푸실듯 2021. 8. 24.
백준 11651 좌표 정렬하기 2 1 2 3 4 5 6 7 8 import sys n=int(sys.stdin.readline()) tmp=[] for i in range(n): a,b=map(int, sys.stdin.readline().split()) tmp.append((a,b)) tmp=sorted(tmp, key= lambda x: (x[1],x[0])) for i in tmp: print(i[0],i[1]) cs lambda로 sort하시면됩니다 2021. 8. 24.
백준 10866 덱 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 from collections import deque import sys ans=deque() n=int(input()) for i in range(n): tmp=list(sys.stdin.readline().split()) if tmp[0]=='push_back' : ans.append(int(tmp[1])) if tmp[0] == 'push_front': ans.appendleft(int(tmp[1])) if tmp[0]=='front' : if len(ans)==0: print(-1) else: print(ans[0]) if tmp[0]=='back' : if len.. 2021. 8. 24.
백준 10845 큐 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 from collections import deque import sys ans=deque() n=int(input()) for i in range(n): tmp=list(sys.stdin.readline().split()) if tmp[0]=='push' : ans.append(int(tmp[1])) if tmp[0]=='front' : if len(ans)==0: print(-1) else: print(ans[0]) if tmp[0]=='back' : if len(ans)==0: print(-1) else: print(ans[-1]) if tmp[0]=='size': print(len(ans)) if.. 2021. 8. 24.
백준 10824 스택 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 from collections import deque import sys ans=deque() n=int(input()) for i in range(n): tmp=list(sys.stdin.readline().split()) if tmp[0]=='push' : ans.append(int(tmp[1])) if tmp[0]=='top' : if len(ans)==0: print(-1) else: print(ans[-1]) if tmp[0]=='size': print(len(ans)) if tmp[0]=='empty': if len(ans)==0 : print(1) else :print (0) if tmp[0]=='pop':.. 2021. 8. 24.
백준 2164 카드 2 1 2 3 4 5 6 7 8 from collections import deque n=int(input()) tmp=deque([i+1 for i in range(n)]) while len(tmp)>1: tmp.popleft() tmpNum=tmp.popleft() tmp.append(tmpNum) print(tmp[0]) cs 1 2 3 4 5 6 7 from collections import deque n=int(input()) tmp=deque([i+1 for i in range(n)]) while len(tmp)>1: tmp.popleft() tmp.rotate(-1) print(tmp[0]) cs 제일 처음 생각했던건 리스트를 써서 pop을 두번하자 였는데 시간초과 나오더라구요 그래서 colle.. 2021. 8. 24.
백준 10814 나이순 정렬 1 2 3 4 5 6 7 8 9 10 import sys n=int(sys.stdin.readline()) tmp=[] for i in range(n): a,b=sys.stdin.readline().split() a=int(a) tmp.append((a,b,i)) tmp=sorted(tmp,key= lambda x: (x[0],x[2]) ) for i in tmp: print(i[0],i[1]) cs 이 문제도 크게 어렵진 않구 다만 들어온 순서 고려하는 부분이 line 7, line8 에 각각 있습니다 2021. 8. 23.
백준 11650 좌표 정렬하기 1 2 3 4 5 6 7 8 9 pythotmp=[] n=int(input()) for i in range(n): a,b=map(int,input().split()) tmp.append((a,b)) tmp=sorted(tmp, key= lambda x: (x[0],x[1])) for i in tmp: print (i[0],i[1]) cs 요거는 lambda 이용해서 sort 두번 하면됩니다! 2021. 8. 23.
백준 2702 초6 수학 1 2 3 4 5 6 7 8 9 10 def gcd(a,b): if(b==0): return a else: return gcd(b,a%b) n=int(input()) for i in range(n): a,b=map(int,input().split()) print(a*b//gcd(a,b),gcd(a,b)) cs 나름 학교다닐때 모의고사보면 수리영역 1등급이였는데 나이먹고 수학관련 문제보니 제목이 초6수학인데도 어지럽네요 여튼 GCD , Greatest Common Divisor 최대공약수를 찾으면 최소공배수까지 쉽게 구할수있죠 2021. 8. 19.
백준 3040 백설 공주와 일곱 난쟁이 1 2 3 4 5 6 7 8 9 10 11 12 tmp=[] for i in range(9): tmp.append(int(input())) for i in range(9): for j in range(i+1,9): if sum(tmp)-tmp[i]-tmp[j]==100: x,y=i,j break tmp.pop(x) tmp.pop(y-1) for i in tmp: print(i) cs x번째 index를 pop하면 리스트의 길이가 하나 줄어드니 y-1번째 index를 pop해야함. list. pop("index") list.remove("value") pop과 remove의 간단한 차이입니다. 2021. 8. 19.