Post

[Unrated] [모의 SW 역량테스트] 숫자 만들기 - 4008

[Unrated] [모의 SW 역량테스트] 숫자 만들기 - 4008

문제 링크

문제 링크

성능 요약

메모리: 13,544 KB, 시간: 19 ms

코드길이: 1,345 Bytes

출처: SW Expert Academy, https://swexpertacademy.com/main/code/problem/problemList.do

코드

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

int T, tc;
int N;
int diff;
int myOperator[4];
int nums[13];

int usingOp;
const int ADD = 0;
const int SUB = 1;
const int MUL = 2;
const int DIV = 3;

int maxVal = -21e8;
int minVal = 21e8;

void init()
{
	memset(myOperator, -1, sizeof(myOperator));
	memset(nums, 0, sizeof(nums));
}

void input()
{
	cin >> N;
	// 연산자 input
	for (int i = 0; i < 4; i++)
	{
		cin >> myOperator[i];
	}
	// 피 연산자 input
	for (int i = 0; i < N; i++)
	{
		cin >> nums[i];
	}
}

void DFS(int lev, int val)
{
	if (lev == N)
	{
		if (maxVal < val)
		{
			maxVal = val;
		}
		if (minVal > val)
		{
			minVal = val;
		}
		diff = maxVal - minVal;
		return;
	}

	for (int i = 0; i < 4; i++)
	{
		if (myOperator[i] == 0) continue;
		myOperator[i]--;
		usingOp = i;
		switch (usingOp)
		{
		case ADD:
			DFS(lev + 1, val + nums[lev]);
			break;
		case SUB:
			DFS(lev + 1, val - nums[lev]);
			break;
		case MUL:
			DFS(lev + 1, val * nums[lev]);
			break;
		case DIV:
			DFS(lev + 1, val / nums[lev]);
			break;
		}
		myOperator[i]++;
	}
}

void solve()
{
	maxVal = -21e8;
	minVal = 21e8;
	DFS(1, nums[0]);
}

void output()
{
	cout << "#" << tc << " " << diff << "\n";
}
int main()
{
	cin >> T;
	for (tc = 1; tc <= T; tc++)
	{
		init();
		input();
		solve();
		output();
	}

	return 0;
}
This post is licensed under CC BY 4.0 by the author.