🐯알고리즘/BOJ

백준 1주차(2)

계란소년 2025. 1. 8. 23:39

4일차(1/9)

 

https://www.acmicpc.net/problem/11005

 

꽤 까다로웠다.

 

n,b = map(int,input().split())
ans=''
while n!=0:

    remainder= n%b
    if remainder>=10:
        buf = chr(remainder - 10 + ord('A'))
    else:
        buf =str(remainder)
    ans = buf + ans
    n//=b
print(ans)

 

https://www.acmicpc.net/problem/3062

 

N=int(input())
for _ in range(N):
    num= int(input())
    num_reverse = int(str(num)[::-1])
    ans= num+num_reverse
    n=len(str(ans))
    awr=0
    for i in range(n):
        if str(ans)[i] == str(ans)[n-1-i]:
            awr+=1
    if awr==n:
        print("YES")
    else:
        print("NO")

 

https://www.acmicpc.net/problem/2596

 

왜 어렵냐...

n = int(input())
arr = input()
words = []

for i in range(n):
    words.append(arr[i * 6:(i + 1) * 6])

dic = {
    "000000": "A",
    "001111": "B",
    "010011": "C",
    "011100": "D",
    "100110": "E",
    "101001": "F",
    "110101": "G",
    "111010": "H"
}

def is_one_bit_different(s1, s2):
    return sum(c1 != c2 for c1, c2 in zip(s1, s2)) == 1

result = ""
for i, word in enumerate(words):
    if word in dic:
        result += dic[word]
    else:
        found = False
        for key, value in dic.items():
            if is_one_bit_different(word, key):
                result += value
                found = True
                break
        if not found:  
            print(i + 1)
            exit()

print(result)

 

5일차(1/10)

 

https://www.acmicpc.net/problem/13235

word= input()

n=len(word)
ans=0

# abccba
# if n%2==0:
for i in range(n):
    if word[i]==word[n-i-1]:
        ans+=1
if ans==n:
    print("true")
else:
    print("false")

 

https://www.acmicpc.net/problem/11179

num=int(input())
bin_num=bin(num)[2:]
reverse_bin_num=''.join(reversed(bin_num))
ans=int(reverse_bin_num,2)
print(ans)

해당 문제 관련 정리

10진수 2진수 변환

# 10진수를 2진수로 변환
num = 10
bin_num = bin(num)[2:]  # '0b' 접두사를 제거하기 위해 [2:] 사용
print(bin_num)  # 출력: '1010'



# 2진수를 10진수로 변환
bin_num = "1010"
decimal_num = int(bin_num, 2)
print(decimal_num)  # 출력: 10

 

문자열, 배열 뒤집기

문자열

#슬라이싱
s = "hello"
reversed_s = s[::-1]
print(reversed_s)  # 출력: 'olleh'

#reversed
s = "hello"
reversed_s = ''.join(reversed(s))
print(reversed_s)  # 출력: 'olleh'



배열

#슬라이싱
arr = [1, 2, 3, 4]
reversed_arr = arr[::-1]
print(reversed_arr)  # 출력: [4, 3, 2, 1]

#reverse
arr = [1, 2, 3, 4]
arr.reverse()
print(arr)  # 출력: [4, 3, 2, 1]

#reversed
arr = [1, 2, 3, 4]
reversed_arr = list(reversed(arr))
print(reversed_arr)  # 출력: [4, 3, 2, 1]

 

 

6일차(1/12)

 

https://www.acmicpc.net/problem/10864

n,m=map(int,input().split()) 
dic={}
for i in range(1,1+n):
    dic[i]=[]
for _ in range(m):
    a,b= map(int,input().split())
    dic[a].append(b)
    dic[b].append(a)

for key, value in dic.items():
    print(len(value))

 

https://www.acmicpc.net/problem/12759

 

처음에 짠 코드

def check_winner_1(board):
    # 가로
    for row in board:
        if row[0] == row[1] == row[2] == 1:
            return 1

    # 세로
    for col in range(3):
        if board[0][col] == board[1][col] == board[2][col] == 1:
            return 1

    # 대각선
    if board[0][0] == board[1][1] == board[2][2] ==1:
        return 1
    if board[0][2] == board[1][1] == board[2][0] ==1:
        return 1

    # 무승부
    return 0

def check_winner_2(board):
    # 가로
    for row in board:
        if row[0] == row[1] == row[2] == 2:
            return 2

    # 세로
    for col in range(3):
        if board[0][col] == board[1][col] == board[2][col] == 2:
            return 2

    # 대각선
    if board[0][0] == board[1][1] == board[2][2] ==2:
        return 2
    if board[0][2] == board[1][1] == board[2][0] ==2:
        return 2

    # 무승부
    return 0

n=int(input())
arr=[ [0,0,0] for _ in range(3)]
if n==2: #2번이 선공
    for i in range(9):
        if i%2==0: #선공
            r,c=map(int,input().split())
            arr[r-1][c-1]=2
        else: #후공
            r,c=map(int,input().split())
            arr[r-1][c-1]=1
elif n==1: #1번이 선공
    for i in range(9):
        if i%2==0: #선공
            r,c=map(int,input().split())
            arr[r-1][c-1]=1
        else: #후공
            r,c=map(int,input().split())
            arr[r-1][c-1]=2
# print(arr)
one=check_winner_1(arr)
two=check_winner_2(arr)

if one==1:
    print(1)
if two==2:
    print(2)
if one==1 and two==2:
    print("??")
if one==two==0:
    print(0)

2개 다 이기는 경우를 해결하지 못함

 

최종 코드

def check_winner(board):
    # 가로
    for row in board:
        if row[0] == row[1] == row[2] != 0:
            return row[0]

    # 세로
    for col in range(3):
        if board[0][col] == board[1][col] == board[2][col] != 0:
            return board[0][col]

    # 대각선
    if board[0][0] == board[1][1] == board[2][2] != 0:
        return board[0][0]
    if board[0][2] == board[1][1] == board[2][0] != 0:
        return board[0][2]

    return 0

n=int(input())
arr=[ [0,0,0] for _ in range(3)]

for i in range(9):
    if n == 1:
        if i%2 ==0:
            player = 1
        else:
            player = 2
    else:
        if i%2 ==0:
            player = 2
        else:
            player = 1
    r, c = map(int, input().split())
    arr[r-1][c-1] = player

    # 매 턴마다 승리 확인
    winner = check_winner(arr)
    if winner != 0:
        print(winner)
        break
else: #무승부
    print(0)