본문 바로가기
알고리즘/알고리즘 설명

시뮬레이션과 창의적 해결

by JayAlex07 2023. 5. 9.

🧑‍💻 시뮬레이션과 창의적 해결

멀티잇 코딩테스트 러닝클래스'Python 5월반

 

 

0 커플

숫자로 된 리스트가 주어진다

  • 리스트 안에 숫자의 개수는 짝수이다
  • 음수와 양수가 들어가 있고, 둘의 합이 0이면 커플이다
  • 그 중 합하면 0이 아닌 숫자를 구해야 한다

딕셔너리로 먼저 해결을 했다

  • key 로는 숫자의 절대값을 넣었고, value에다가 원래 값을 넣었다
  • 만약 절대값이 같으면 value에 값끼리 더했다 (같은 절대값이 나오면, value는 0이 된다)
  • 그리고 마지막에 모든 value 들을 더했다
  • 짝이 없으면, value는 0이 아니라, 다른 숫자일 것

 

N = int(input())
score = list(map(int, input().split()))

temp_dict = {}

for s in score:
    if abs(s) not in temp_dict:
        temp_dict[abs(s)] = s
    elif abs(s) in temp_dict:
        temp_dict[abs(s)] += s

print(sum(temp_dict.values()))

 

 

폭탄 구현하기

폭탄이 떨어진 좌표와, 위 아래 좌 우 방면의 좌표에 1을 더해주면 된다

 

위 아래 좌 우 좌표를 구하기 위해 Delta 탐색을 사용하였다

 

N, K = map(int, input().split())

dr = [-1, 0, 0, 1]
dc = [0, -1, 1, 0]
field = [[0] * N for _ in range(N)]

for _ in range(K):
    y, x = map(int, input().split())
    y, x = y - 1, x - 1
    field[y][x] += 1

    for i in range(4):
        sr = dr[i] + y
        sc = dc[i] + x

        if 0 <= sr < N and 0 <= sc < N:
            field[sr][sc] += 1

answer = 0

for f in field:
    answer += sum(f)

print(answer)

 

 

규칙 숫자 야구

answer = list(map(int, input()))
user_input = list(map(int, input()))

count = 0

def fail():
    for i in range(4):
        if result[i] == "Fail":

            while True:
                temp_num = (user_input[i] + 1) % 10
                out = temp_num not in user_input
                user_input[i] = temp_num

                if out:
                    break


def ball():
    if "Ball" not in result:
        return

    pos = []
    value = []
    for i in range(4):
        if result[i] != "Strike":
            pos.append(i)
            value.append(user_input[i])

    for i in range(len(pos)):
        if i == 0:
            user_input[pos[i]] = value[-1]
        else:
            user_input[pos[i]] = value[i - 1]


while True:
    result = ["Fail", "Fail", "Fail", "Fail"]
    count += 1

    if answer == user_input:
        print(count)
        break

    else:
        for i in range(4):
            if answer[i] == user_input[i]:
                result[i] = "Strike"
            elif answer[i] != user_input[i] and user_input[i] in answer:
                result[i] = "Ball"

    fail()
    ball()

'알고리즘 > 알고리즘 설명' 카테고리의 다른 글

기초 자료구조의 구현과 응용  (0) 2023.05.22
완전 탐색  (0) 2023.05.11
기초 수학과 구현  (0) 2023.05.05
기초 문자열 구현  (0) 2023.05.04
Udemy : 동적 계획법 (DP)  (0) 2023.03.23