SW Expert Academy - 1926. 간단한 369게임

2024. 4. 17. 21:34코테 준비!

#문제링크

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AV5PTeo6AHUDFAUq&categoryId=AV5PTeo6AHUDFAUq&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=2&pageSize=10&pageIndex=1

 

n = int(input())
for i in range(1,n+1):
    data = list(str(i))
    #print(i)
    cnt = 0
    for j in range(len(data)):
        if data[j]=="3" or data[j]=="6" or data[j]=="9":
            cnt += 1
    if cnt == 1:
        print("-", end=" ")
    elif cnt ==2:
        print("--", end=" ")
    else:
        print(i,end=" ")

 

1. 정수 입력받기!

2. 정수만큼 반복합니다.

3. data = list(str(i))를 통해서 i의 값을 data에 추가합니다

    3-1 i = 1 ['1'],  i = 2 ['2'] . . . i = 10 ['1', '0'] 입니다. 문자열을 통해서 전 3, 6 ,9를 비교해서 찾을 겁니다!

4. 만약 33인 경우 -- 박수를 2번 쳐야 하므로 cnt를 0으로 초기화 시켜줍니다.

5. for j in range(len(data))를 통해서 data의 길이만큼 반복합니다. 그래야 10인 경우 2번의 비교가 가능합니다.

6. if data[j]=="3" or data[j]=="6" or data[j]=="9" 이면 cnt +=1 해줍니다.

7. 출력 부분입니다. cnt == 1이면 - 출력, 2이면 --출력, 1,2가 아니라면 반복하고 있는 i의 값을 출력합니다.

 

 

GPT 피드백!