Notice
Recent Posts
Recent Comments
Link
Star_project
코딩테스트 2. 코드 구현능력 기르기 본문
출처 : 인프런 강의
강의명 : 파이썬 알고리즘 문제풀이 (코딩테스트 대비)
import sys
sys.stdin=open("pythonalgorithm/섹션 2/1. k번째 약수/in1.txt", "rt")
n,k=map(int,input().split())
cnt=0
for i in range(1, n+1):
if n%i==0:
cnt+=1
if cnt==k:
print(i)
break
else:
print(-1)
K번째 수
T=int(input())
for t in range(T):
n, s, e, k=map(int, input().split())
a=list(map(int, input().split()))
a=a[s-1:e]
a.sort()
print("# %d %d" %(t+1, a[k-1]))
K번째 큰 수
import sys
sys.stdin=open("pythonalgorithm/섹션 2/3. k번째 큰 수/in2.txt", "rt")
n, k=map(int, input().split())
a=list(map(int, input().split()))
res=set()
for i in range(n):
for j in range(i+1, n):
for m in range(j+1, n):
res.add(a[i]+a[j]+a[m])
res=list(res)
res.sort(reverse=True)
print(res[k-1])
대표값
import sys
sys.stdin=open("pythonalgorithm/섹션 2/4. 대표값/in4.txt", "rt")
n=int(input())
a=list(map(int, input().split()))
ave=round(sum(a)/n)
min=2147000000
for idx, x in enumerate(a):
tmp=abs(x-ave)
if tmp<min:
min=tmp
score=x
res=idx+1
elif tmp==min:
if x>score:
score=x
res=idx+1
print(ave, res)
정다면체
import sys
sys.stdin=open("pythonalgorithm/섹션 2/5. 정다면체/in1.txt","r")
n, m=map(int, input().split())
cnt=[0]*(n+m+3)
max=-2147000000
for i in range(1, n+1):
for j in range(1, m+1):
cnt[i+j]+=1
for i in range(n+m+1):
if cnt[i]>max:
max=cnt[i]
for i in range(n+m+1):
if cnt[i]==max:
print(i, end=' ')
자릿수의 합
import sys
sys.stdin=open("pythonalgorithm/섹션 2/6. 자릿수의 합/in1.txt","r")
n=int(input())
a=list(map(int, input().split()))
# 방법1
def digit_sum(x):
sum=0
while x>0:
sum+=x%10 ## 나머지
x=x//10 ## 몫
return sum
max=-2147000000
for x in a:
tot=digit_sum(x)
if tot>max:
max=tot
res=x
print(res)
#방법2
def digit_sum(x):
sum=0
for i in str(x):
sum+=int(i)
return sum
max=-2147000000
for x in a:
tot=digit_sum(x)
if tot>max:
max=tot
res=x
print(res)
소수(에라토스테네스 체)
import sys
sys.stdin=open("pythonalgorithm/섹션 2/7. 소수(에라토스테네스 체)/in3.txt", "rt")
n=int(input())
ch=[0]*(n+1)
cnt=0
for i in range(2, n+1):
if ch[i]==0:
cnt+=1
for j in range(i, n+1, i):
ch[j]=1
print(cnt)
뒤집은 소수
import sys
sys.stdin=open("pythonalgorithm/섹션 2/8. 뒤집은 소수/in3.txt", "rt")
n=int(input())
a=list(map(int, input().split()))
def reverse(x):
res=0
while x>0:
t=x%10
res=res*10+t
x=x//10
return res
def isPrime(x):
if x==1:
return False
for i in range(2, x//2+1):
if x%i==0:
return False
else:
return True
for x in a:
tmp=reverse(x)
if isPrime(tmp):
print(tmp, end=' ')
주사위 게
import sys
sys.stdin=open("pythonalgorithm/섹션 2/9. 주사위 게임/in4.txt", "rt")
n=int(input())
res=0
for i in range(n):
tmp=input().split()
tmp.sort()
a, b, c=map(int, tmp)
if a==b and b==c:
money=10000+1000*a
elif a==b or a==c:
money=1000+100*a
elif b==c:
money=1000+100*b
else:
money=100*c
if money>res:
res=money
print(res)
점수계산
import sys
sys.stdin=open("pythonalgorithm/섹션 2/10. 점수 계산/in2.txt", "rt")
n=int(input())
a=list(map(int, input().split()))
cnt=0
sum=0
for x in a:
if x==1:
cnt+=1
sum+=cnt
else:
cnt=0
print(sum)'코딩테스트 > Python' 카테고리의 다른 글
| 코딩테스트 3. 탐색 & 시뮬레이션 (2) (0) | 2022.06.06 |
|---|---|
| 코딩테스트 3. 탐색 & 시뮬레이션 (1) (0) | 2022.06.03 |
| TypeError: 'int' object is not callable (0) | 2022.06.02 |
| set 자료구조로 으로 중복제거, list 자료구조로 sort (0) | 2022.06.02 |
| sys.stdin=open() map(int, input().split()) list(map(int, input().split() (0) | 2022.06.02 |