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

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

<풀이>

1. 8개의 숫자를 입력받는다.

2. 주어진 작업을 종료된 후 8개의 숫자를 출력한다.

 

<해법>

1. 하나하나씩 다 해봐야 할까?

=> 사실 이 문제를 푸는 것은 간단합니다. 큐를 이용해서 주어진 조건에 맞게 구현하는 것은 어렵지 않은 일입니다. 하지만, 테스트케이스처럼 큰 숫자가 나올경우 너무 오래걸릴거란 생각이 듭니다. 꼭 하나하나씩 1,2,3,4,5,1,2,3,4,5... 순서로 빼봐야할까요? 아닙니다. 잘 생각해보면, 8사이클이 돌 경우 모든 숫자에서 1+2+3+4+5 = 15의 숫자가 빠지게 됩니다. 따라서, 모든 숫자에서 15씩 동일하게 여러번 뺄 수 있습니다. 그렇지만, 여기서 조금 더 생각해서 숫자가 15,30,45 등등 15의 배수일 경우를 생각해야합니다. 이 때는 0이 될때까지 빼면 안됩니다. 왜냐하면 0은 사이클의 종료조건이기 때문입니다. 그러므로, 이 경우에는 한번을 덜 빼야합니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string.h>
#include <queue>
#define INF 987654321
 
using namespace std;
 
int trash;
int arr[8];
queue<int> q;
int minimum;
 
void solution() {
    int div = minimum / 15;
    int remainder = minimum % 15;
    div = (remainder == 0) ? div - 1 : div;
    for (int i = 0; i < 8; i++) {
        arr[i] -= (15 * div);
    }
    for (int i = 0; i < 8; i++) {
        q.push(arr[i]);
    }
    while (true) {
        for (int i = 1; i <= 5; i++) {
            int tmp = q.front() - i;
            q.pop();
            q.push((tmp <= 0 ? 0 : tmp));
            if (tmp <= 0return;
        }
    }
}
 
int main() {
    int test_case;
    int T;
 
    T = 10;
 
    for (test_case = 1; test_case <= T; test_case++) {
 
        //초기화
        trash = 0;
        memset(arr, 0sizeof(arr));
        while (!q.empty()) q.pop();
        minimum = INF;
 
        //입력
        cin >> trash;
        for (int i = 0; i < 8; i++) {
            int num;
            cin >> num;
            arr[i] = num;
            minimum = min(minimum, num);
        }
 
        //해법
        solution();
 
        //출력
        cout << "#" << test_case << " ";
        while (!q.empty()) {
            cout << q.front() << " ";
            q.pop();
        }
        cout << "\n";
    }
 
    //종료
    return 0;
}

 

수학적인 사고와 구현에 대해 알아볼 수 있었습니다.

'알고리즘 문제풀이 > SWEA' 카테고리의 다른 글

[C++] SWEA 5644 - 무선 충전(2)  (0) 2022.12.24
[C++] SWEA 1227 - 미로2  (0) 2022.12.24
[C++] SWEA 5650 - 핀볼 게임(2)  (0) 2022.12.23
[C++] SWEA 1226 - 미로1  (0) 2022.12.21
[C++] SWEA 1224 - 계산기3  (0) 2022.12.21

+ Recent posts