[17 JAN 2024] SILVER 5

합이·2024년 1월 17일
0

Coding Test

목록 보기
2/2

1316 번:

    for i in range(len(word)-1):
        if word[i] != word[i+1]:
            new = word[i+1:]
            if new.count(word[i]) > 0:
                error += 1

range(len(word)-1) 인 이유: 두 글자씩 비교해나가는 과정인데, 만약 맨 마지막 index까지 가면 비교할 대상이 없어진다. 그럼 error가 발생.

25206번:

grades = ['A+','A0','B+','B0','C+','C0','D+','D0','F']
scores = [4.5, 4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0.0]
credit_total = 0
total = 0

for _ in range(20):
    s, c, g = input().split()
    c = float(c)
    if g != 'P':
        credit_total += c
        total += c * scores[grades.index(g)]

print('%6f'%(total / credit_total))

scores[grades.index(g)]: 문자열의 index를 반환하여 scores 리스트의 값을 반환할 수 있다.

print('%6f'%(total / credit_total)): '%nf'% 뒤에 출력을 붙이면 원하는 소수점까지 나타낼 수 있다. 자세한 내용은 아래 링크 참조.
https://dogsavestheworld.tistory.com/entry/python-소수-n째-자리까지-출력하기-round-format-f-string

2563번:

n = int(input())
paper = [[0]*100 for _ in range(100)]
result = 0

for _ in range(n):
    x, y = map(int, input().split())

    for i in range(y,y+10):
        for j in range(x,x+10):
            paper[i][j] = 1

for n in range(100):
    result += paper[n].count(1)

print(result)

paper = [[0]*100 for _ in range(100)]: 이렇게 한줄로도 2차원 배열을 간단히 만들 수 있다.

paper[i][j]: 모든 요소가 0인 배열을 만든 후에 해당되는 것들만 1로 바꾸어 면적을 찾는 방식은 정말 새롭고 신기했다. 이런 아이디어를 기억해놔야할 것 같다.

1193번:

N = int(input())
line = 1

while N > line:
    N -= line
    line += 1
    
if line % 2 == 0:
    a = N
    b = line - N + 1

elif line % 2 == 1:
    a = line - N + 1
    b = N

print(str(a)+'/'+str(b))

9506번:

while 1:
    n = int(input())
    perfect = []
    count = 0

    if n == -1:
        break

    for i in range(1, n+1):
        if n % i == 0:
            perfect.append(i)
            count += 1
    
    result = " + ".join(map(str, perfect[0:count-1]))

    if sum(perfect[0:count-1]) == n:
        print(n, '=',result)        

    else:
        print(n,'is NOT perfect.')
profile
열심히

0개의 댓글