본문 바로가기

브루트 포스/브루트포스(순열)

연산자 끼워넣기

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

 

14888번: 연산자 끼워넣기

첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1인 4개의 정수가 주어지는데, 차례대로 덧셈(+)의 개수, 뺄셈(-)의 개수, 

www.acmicpc.net

로또 문제 기억하는가?

같은 문자가 포함된 순열

 

내 코드

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
#include <iostream>
#include <algorithm>
#include <vector>
 
using namespace std;
 
int op[5];
 
int calc(vector<int>& num, vector<int>& d)
{
    int sum = num[0];
    int now = 0;
    for (int i = 0; i < d.size(); i++)
    {
        if (d[i] == 0) {
            sum = sum + num[i + 1];
        }
        else if (d[i] == 1) {
            sum = sum - num[i + 1];
        }
        else if (d[i] == 2) {
            sum = sum * num[i + 1];
        }
        else if (d[i] == 3) {
            sum = sum / num[i + 1];
        }
    }
 
    return sum;
}
 
 
int main()
{
    int n;
    cin >> n;
    vector<int> num(n);
 
    for (int i = 0; i < n; i++)
    {
        cin >> num[i];
    }
 
    for (int i = 0; i < 4; i++)
    {
        cin >> op[i];
    }
 
    vector<int> d;
    for (int i = 0; i < op[0]; i++)
        d.push_back(0);
 
    for (int i = 0; i < op[1]; i++)
        d.push_back(1);
 
    for (int i = 0; i < op[2]; i++)
        d.push_back(2);
 
    for (int i = 0; i < op[3]; i++)
        d.push_back(3);
 
    int min = 100000000;
    int max = -100000000;
    int now;
    do {
        now = calc(num, d);
        if (min > now)
            min = now;
        if (max < now)
            max = now;
    } while (next_permutation(d.begin(), d.end()));
 
    cout << max << '\n' << min;
}
cs

 

답지 코드

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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int calc(vector<int> &a, vector<int> &b) {
    int n = a.size();
    int ans = a[0];
    for (int i=1; i<n; i++) {
        if (b[i-1== 0) {
            ans = ans + a[i];
        } else if (b[i-1== 1) {
            ans = ans - a[i];
        } else if (b[i-1== 2) {
            ans = ans * a[i];
        } else {
            ans = ans / a[i];
        }
    }
    return ans;
}
 
int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i=0; i<n; i++) {
        cin >> a[i];
    }
    vector<int> b;
    for (int i=0; i<4; i++) {
        int cnt;
        cin >> cnt;
        for (int k=0; k<cnt; k++) {
            b.push_back(i);
        }
    }
    sort(b.begin(),b.end());
    vector<int> result;
    do {
        int temp = calc(a, b);
        result.push_back(temp);
    } while (next_permutation(b.begin(), b.end()));
    auto ans = minmax_element(result.begin(), result.end());
    cout << *ans.second << '\n';
    cout << *ans.first << '\n';
    return 0;
}
cs

'브루트 포스 > 브루트포스(순열)' 카테고리의 다른 글

  (0) 2023.09.12
스타트와 링크 (순열)  (0) 2023.03.20
단어 수학 (중요!!)  (0) 2023.03.18
부등호 (순열 풀이)  (0) 2023.03.18
로또 (같은 문자를 포함한 순열)  (0) 2023.02.23