본문 바로가기

python-algorithm1422

백준 12788 제 2회 IUPC는 잘 개최될 수 있을까? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 n=int(input()) m,k=map(int, input().split()) cntPencil=m*k ctpList=sorted( list(map(int,input().split())) ,reverse=True) start=0 currentPencil=0 flag=0 for i in ctpList: currentPencil+=i start+=1 if currentPencil>= cntPencil: print(start) flag=1 break if flag==0: print("STRESS") Colored by Color Scripter cs 2022. 1. 1.
백준 1235 학생 번호 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 n=int(input()) studentList=[input() for i in range(n)] numberLen=len(studentList[0]) # print(studentList) # print(numberLen) for i in range(1,numberLen+1): compareList=[] for j in range(n): if studentList[j][numberLen-i:numberLen] in compareList: break else: compareList.append(studentList[j][numberLen-i:numberLen]) #print(compareList) if len(compareList)==n.. 2022. 1. 1.
백준 13171 A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 a=int(input()) x=int(input()) ans=1 xtobin=bin(x)[2:][::-1] tmp=[] for i in range(65): tmp.append(a) a=a**2 a%=1000000007 for i in range(len(xtobin)): if int(xtobin[i])==1: ans*=tmp[i] ans%=1000000007 print(ans) cs 2021. 12. 30.
백준 17952 과제는 끝나지 않아! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #17952 import sys n=int(input()) assignmentList=[] score=0 for i in range (n): task=list(map(int, sys.stdin.readline().split())) if task[0]==1: assignmentList.append([task[1],task[2]]) if assignmentList: assignmentList[-1][1]-=1 if assignmentList[-1][1]==0: score += assignmentList[-1][0] assignmentList.pop() print(score) Colored by Color Scripter cs 2021. 12. 30.
백준 11899 괄호 끼워넣기 1 2 3 4 5 s=input() n=len(s)//2 for _ in range(n): s=s.replace("()","") print(len(s)) cs replace()함수 사용해서 쉽게 구현했고 Line2에서 2로 나눈 이유는 "()"가 길이를 2만큼 차지해서 굳이 n번만큼 반복안하고 n번의 절반만큼만 반복해도 가능함 2021. 12. 29.
백준 11332 시간초과 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import math import sys def solution(oper,n,t,l): n,t,l=int(n),int(t),int(l) oneSecond=(10**8)*l if oper=='O(N)': bigO=n*t elif oper=='O(2^N)': bigO=pow(2,n)*t elif oper == 'O(N!)': bigO=math.factorial(n)*t elif oper == 'O(N^3)': bigO=pow(n,3)*t elif oper == 'O(N^2)': bigO = pow(n, 2) * t if oneSecond(10**8)*l : return False return True def so.. 2021. 12. 28.
백준 2729 이진수 덧셈 1 2 3 4 5 6 7 8 9 10 11 12 def deciTobin(a): lenA=len(a) numA=0 for i in range(lenA): numA+=int(a[i])*pow(2,lenA-i-1) return numA n=int(input()) for _ in range(n): ans=[0]*81 a,b=input().split() print(bin(deciTobin(a)+deciTobin(b) )[2:]) Colored by Color Scripter cs 2021. 12. 28.
백준 2870 수학숙제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 nums=[] n=int(input()) for _ in range (n): s = input() tmpNum='' for i in s: if i.isnumeric() : tmpNum+=str(i) if i.isalpha(): if tmpNum=='':continue else: nums.append(int(tmpNum)) tmpNum='' if tmpNum!='' : nums.append(int(tmpNum)) for i in sorted(nums): print(i) Colored by Color Scripter cs 2021. 12. 28.
백준 1822 차집합 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import sys a,b=map(int, sys.stdin.readline().split()) set1=set(map(int,sys.stdin.readline().split())) set2=set(map(int,sys.stdin.readline().split())) cnt=0 ans=[] for i in set1: if i not in set2: cnt+=1 ans.append(i) print(cnt) if cnt!=0 : ans.sort() print(*ans) Colored by Color Scripter cs 문제에서 집합이라고 말했으니 처음부터 자료구조를 set로 써서 구현하면 쉽게 구합니다 https://stackoverflow.com.. 2021. 12. 27.
백준 13900 순서쌍의 곱의 합 1 2 3 4 5 6 n=int(input()) nums=list(map(int,input().split())) ans=0 for i in range(n-1): ans+=nums[i]*sum(nums[i+1:]) print(ans) cs 1 2 3 4 5 6 7 8 9 import sys n=int(input()) nums=list(map(int,sys.stdin.readline().split())) sumNums=sum(nums) ans=0 for i in nums: sumNums-=i ans+=i*sumNums print(ans) Colored by Color Scripter cs 처음엔 수학적으로 그 i번째 숫자 * (i+1번째부터 마지막항까지의 합) 구하면 O(N)으로 구할수있겠다 싶었는데 시간초.. 2021. 12. 27.
백준 11508 2+1 세일 1 2 3 4 5 6 7 8 9 10 11 12 n=int(input()) nums=[int(input()) for i in range(n)] cnt=0 nums.sort(reverse=True) ans=0 while n>0: target=nums[cnt] ans+=target cnt+=1 if cnt%3==0 :ans-=target n-=1 print(ans) cs 내림차순으로 정리하고 3의 배수번째는 가격에 합산안해주면 됩니다 2021. 12. 27.
백준 13699 점화식 1 2 3 4 5 6 7 t=[1]*36 for i in range(1,36): target=0 for j in range(i): target+=t[j]*t[i-(j+1)] t[i]=target print(t[int(input())]) cs 이 문제는 사실 DP로 안풀어도 구현가능해서.. 2021. 12. 27.