For Programmer

2. 구현(Implementation) 문제 본문

코팅테스트/코딩테스트 이론 정리

2. 구현(Implementation) 문제

유지광이 2021. 8. 13. 11:58
728x90

문제2

나의코드

N = int(input())

count = 0

for i in range(N+1):
    for m in range(60):
        for s in range(60):
            if(s == 3 or m == 3 or i == 3):
                count += 1
            elif(s == 13 or m == 13 or i == 13):
                count +=1
            elif(s== 23 or m == 23 or i == 23):
                count +=1
            elif (s == 43 or m == 43):
                count += 1
            elif (s == 53 or m == 53):
                count += 1
            elif ((s >= 30 and s<=39) or (m >= 30 and m<=39)):
                count += 1
print(count)

정답코드 1

N = int(input())

count = 0

for i in range(N+1):
    for m in range(60):
        for s in range(60):
            if '3' in str(i)+str(m)+str(s): #i m s 를 문자로 바꿔버린다.
                count += 1
print(count)

정답코드2

N = int(input())

count = 0

for i in range(N+1):
    for m in range(60):
        for s in range(60):
            if( i % 10 == 3 or i // 10 == 3 or m % 10 == 3 or m // 10 == 3 or s % 10 == 3 or s // 10 == 3):
                count += 1
print(count)

문제3

나의 코드

#현재 나이트의 위치 입력받기
input_data = input()
row = int(input_data[1])
column = int(ord(input_data[0])) - int(ord('a')) + 1

dx=list(map(int,[0,0,+1,-1])) #동 서 남 북 ,행
dy=list(map(int,[1,-1,0,0])) #동 서 남 북 ,열
count = 0



#경우의수 동동남,동동북,서서남,서서북,남남서,남남북,북북서,북북동
nx = column + dy[0] + dy[0] #동동남
ny = row + dx[2]
if(nx < 8 and ny < 8 ):
    count += 1
nx = column + dy[0] + dy[0] #동동북
ny = row + dx[3]
if(nx < 8 and ny > 0 ):
    count += 1
nx = column + dy[1] + dy[1] #서서남
ny = row + dx[2]
if(nx > 0 and ny < 8 ):
    count += 1
nx = column + dy[1] + dy[1] #서서북
ny = row + dx[3]
if(nx > 0 and ny > 0 ):
    count += 1
nx = column + dy[1] #남남서
ny = row + dx[2] + dx[2]
if(nx > 0 and ny < 8 ):
    count += 1
nx = column + dy[0]  #남남동
ny = row + dx[2] + dx[2]
if(nx < 8 and ny < 8 ):
    count += 1
nx = column + dy[1] #북북서
ny = row + dx[3] + dx[3]
if(nx > 0 and ny > 0 ):
    count += 1
nx = column + dy[0]  #북북동
ny = row + dx[3] + dx[3]
if(nx < 8 and ny > 0 ):
    count += 1

print(count)
input_data = input()
row = int(input_data[1])
column = int(ord(input_data[0])) - int(ord('a')) + 1 #아스키코드를 구하여 첫번째 a의 아스키값을 찾아 빼주고 + 1 한다.
result =0

# 나이트가 이동할 수 있는 8가지 방향 정의
steps = [(-2,-1),(-1,-2),(1,-2),(2,-1),(2,1),(1,2),(-1,2),(-2,1)] #2차원 배열을 이용한 방향벡터이용
# 북북서,서서북,서서남,남남서,남남동,동동남,동동북,북북동
dx=[-2,-1,1,2,2,1,-1,-2] #행 방향 
dy=[-1,-2,-2,-1,1,2,2,1] #열 방향


#8가지 방향에 대하여 각 위치로 이동이 가능한지 확인
for step in steps:
    #이동하고자 하는 위치 확인
    next_row = row + step[0]
    next_column = column + step[1]
    #해당 위치로 이동이 가능하다면 카운트 증가
    if next_row >=1 and next_column <= 8 and next_column >=1 and next_column <= 8:
        result += 1

# for i in range(8):
#     next_row = row + dx[i]
#     next_column = column + dy[i]
#     # 해당 위치로 이동이 가능하다면 카운트 증가
#     if next_row >= 1 and next_column <= 8 and next_column >= 1 and next_column <= 8:
#         result += 1
print(result)

문제4

나의 코드

input_data = input()

sdata = []
sum = 0
 # 65 ~ 90 'A' ~ 'Z'
for i in range(len(input_data)):
    if(ord(input_data[i])>= 65 and ord(input_data[i]) <= 90):
        sdata.append(input_data[i])
    else:
        sum += int(input_data[i])
sdata.sort() #정렬

if(sum != 0):
    sdata.append(str(sum))

for data in sdata:
    print(data,end="")

정답 코드

input_data = input()
result=[]
sum =0

#문자를 하나씩 확인하며
for x in input_data:
    #알파벳인 경우 결과 리스트에 삽입
    if x.isalpha():
        result.append(x)
    #숫자는 따로 계산하기
    else:
        sum += int(x)

#알파벳을 오름차순으로 정렬
result.sort()

#숫자가 하나라도 존재하는 경우 가장 뒤에 삽입
if sum != 0:
    result.append(str(sum))

#최종 결과 출력(리스트를 문자열로 변환하여 출력)
print(''.join(result))

구분자를 기준으로 문자를 합쳐주는 함수 '구분자'.join(리스트) , 알파벳이 존재하는지 확인해주는 함수 .isalpha (단,문자열자체를 검사할때는 공백이나 다른기호가 없어야 True를 반환)

728x90
Comments