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

 

6603번: 로또

문제 독일 로또는 {1, 2, ..., 49}에서 수 6개를 고른다. 로또 번호를 선택하는데 사용되는 가장 유명한 전략은 49가지 수 중 k(k>6)개의 수를 골라 집합 S를 만든 다음 그 수만 가지고 번호를 선택하는

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
#include <iostream>
#include <vector>
#include <algorithm>
 
int k = 0;
std::vector<int> vec, lotto;
int ans = 0;
 
bool input()
{
    ans = 0;
    int input;
    vec.clear();
    lotto.clear();
    std::cin >> k;
    for (int i = 0; i < k; ++i)
    {
        std::cin >> input;
        vec.push_back(input);
    }
    return k;
}
 
void solve()
{
    lotto = { 000000 };
    for (int i = 0; i < k - 6++i)
    {
        lotto.push_back(1);
    }
    //std::sort(lotto.begin(), lotto.end());
    do 
    {
        for (int i = 0; i < k; ++i)
        {
            if (lotto[i] == 0)
            {
                std::cout << vec[i] << ' ';
            }
        }
        std::cout << '\n';
    } while (std::next_permutation(lotto.begin(), lotto.end()));
    std::cout << '\n';
}
 
int main()
{
    while (input())
    {
        solve();
    }
    return 0;
}
cs

 

재귀를 쓰지는 않았고, next_permutation 사용했다

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    std::vector<int> tmp1 = { 123 };
    std::vector<int> tmp2 = { 001 };
    do 
    {
        for (auto& it : tmp1)
        {
            std::cout << it << ' ';
        }
        std::cout << '\n';
    } while (std::next_permutation(tmp1.begin(), tmp1.end()));
    std::cout << "-------------------------------------------------\n";
    do
    {
        for (auto& it : tmp2)
        {
            std::cout << it << ' ';
        }
        std::cout << '\n';
    } while (std::next_permutation(tmp2.begin(), tmp2.end()));
cs

원리는 다음과 같다

같은 숫자가 존재한다면 빼버린다

 

a b c

0 0 1

 

b a c

0 0 1

 

이러한 경우는 한번만 출력

 

 

a b

a c

b c

를 출력하면서 3개의 숫자 중 2개의 숫자를 뽑는 조합인 것

 

결론은 next_permutation을 이용한 조합의 구현.

'알고리즘' 카테고리의 다른 글

koi : 연구활동 가는 길(L)  (0) 2020.06.26
koi : 저울 추(L)  (0) 2020.06.26
백준 14888 : 연산자 끼워넣기  (0) 2020.06.23
백준 14502 : 연구소 (브루트포스)  (0) 2020.06.19
백준 17478 : 재귀함수가 뭔가요?  (0) 2020.06.19

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
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
 
int n;
long long maxi = -987654321, mini = 987654321;
std::vector<int> oper;
std::vector<int> vec;
 
void input()
{
    int input;
    std::cin >> n;
    for (int i = 0; i < n; ++i)
    {
        std::cin >> input;
        vec.push_back(input);
    }
    std::cin >> input;
    oper.insert(oper.end(), input, 1);
    std::cin >> input;
    oper.insert(oper.end(), input, 2);
    std::cin >> input;
    oper.insert(oper.end(), input, 3);
    std::cin >> input;
    oper.insert(oper.end(), input, 4);
}
 
void solve()
{
 
    do 
    {
        int sum = vec[0];
        for (int i = 0; i < oper.size(); ++i)
        {
            if (oper[i] == 1)
            {
                sum += vec[i + 1];
            }
            else if (oper[i] == 2)
            {
                sum -= vec[i + 1];
            }
            else if (oper[i] == 3)
            {
                sum *= vec[i + 1];
            }
            else
            {
                sum /= vec[i + 1];
            }
        }
        maxi = maxi < sum ? sum : maxi;
        mini = mini > sum ? sum : mini;
    } while (std::next_permutation(oper.begin(), oper.end()));
    std::cout << maxi << '\n' << mini << std::endl;
}
 
int main()
{
    input();
    solve();
}
cs

 

왜 연산자 입력을 저렇게 했냐면

 

stl 연습이라고 해야할까ㅎ

 

해당 조건에 따라 나눗셈 연산을 제대로 해본다면

 

1
2
3
4
5
6
7
8
9
10
11
12
13
sum = sum < 0 ? -((-sum) / vec[i + 1] ) : sum / vec[i + 1];
 
풀어서 쓰자면
if(sum < 0)
{
    sum = -sum;            //음수를 양수로 바꾸고(절댓값)
    sum /= vec[i + 1]     //바꾼 양수를 나눠서
    sum = -sum;            //다시 음수로 바꾼다.
}
else
{
    sum /= vec[i + 1];
}
cs

 

'알고리즘' 카테고리의 다른 글

koi : 저울 추(L)  (0) 2020.06.26
백준 6603 : 로또  (0) 2020.06.23
백준 14502 : 연구소 (브루트포스)  (0) 2020.06.19
백준 17478 : 재귀함수가 뭔가요?  (0) 2020.06.19
탐색공간의 배제  (0) 2020.06.18

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

 

14502번: 연구소

인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크�

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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <vector>
 
int n, m;
int lab[10][10];
int myLab[10][10];
std::vector<std::pair<intint>> covid19;
int ans = 0;
 
int dy[4= { 010-1 };
int dx[4= { 10-10 };
 
void print()
{
    std::cout << '\n';
    for (int y = 0; y <= n + 1++y)
    {
        for (int x = 0; x <= m + 1++x)
        {
            std::cout << myLab[y][x] << ' ';
        }
        std::cout << '\n';
    }
    std::cout << "------------------------------------\n";
}
 
void input()
{
    std::cin >> n >> m;
    for (int y = 1; y <= n; ++y)
    {
        for (int x = 1; x <= m; ++x)
        {
            std::cin >> lab[y][x];
            if (lab[y][x] == 2)
            {
                covid19.push_back(std::pair<intint>({ y, x }));
            }
            myLab[y][x] = lab[y][x];
        }
    }
}
 
void ff(int y, int x)
{
    if (y < 1 || n < y || x < 1 || m < x)
    {
        return;
    }
    
    myLab[y][x] = 2;
    for (int i = 0; i < 4++i)
    {
        if (myLab[y + dy[i]][x + dx[i]] == 0)
        {
            ff(y + dy[i], x + dx[i]);
        }
    }
}
 
void clear()
{
    for (int y = 1; y <= n; ++y)
    {
        for (int x = 1; x <= m; ++x)
        {
            if (myLab[y][x] == 2)
            {
                myLab[y][x] = 0;
            }
        }
    }
    
    for (auto& it : covid19)
    {
        myLab[it.first][it.second] = 2;
    }
}
 
void solve(int stk)
{
    if (stk == 3)
    {
        //바이러스 플러드필
        for (auto& it : covid19)
        {
            ff(it.first, it.second);
        }
        //print();
 
        //남은 0 체크
        int safety = 0;
        for (int y = 1; y <= n; ++y)
        {
            for (int x = 1; x <= m; ++x)
            {
                if (myLab[y][x] == 0)
                {
                    ++safety;
                }
            }
        }
 
        ans = ans < safety ? safety : ans;
        clear();
        //print();
        return;
    }
    //모든 0에 벽 3개 설치
    for (int y = 1; y <= n; ++y)
    {
        for (int x = 1; x <= m; ++x)
        {
            if (myLab[y][x] == 0)
            {
                myLab[y][x] = 1;
                //print();
                solve(stk + 1);
                myLab[y][x] = 0;
            }
        }
    }
 
}
 
void output()
{
    std::cout << ans;
}
 
int main()
{
    input();
    solve(0);
    output();
    return 0;
}
cs

 

가로세로 바꿔써서 삽질한건 안자랑

'알고리즘' 카테고리의 다른 글

백준 6603 : 로또  (0) 2020.06.23
백준 14888 : 연산자 끼워넣기  (0) 2020.06.23
백준 17478 : 재귀함수가 뭔가요?  (0) 2020.06.19
탐색공간의 배제  (0) 2020.06.18
백준 14501 : 퇴사  (0) 2020.06.18

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

 

17478번: 재귀함수가 뭔가요?

평소에 질문을 잘 받아주기로 유명한 중앙대학교의 JH 교수님은 학생들로부터 재귀함수가 무엇인지에 대하여 많은 질문을 받아왔다. 매번 질문을 잘 받아주셨던 JH 교수님이지만 그는 중앙대��

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
#include <iostream>
 
int n;
 
void input()
{
    std::cin >> n;
    std::cout << "어느 한 컴퓨터공학과 학생이 유명한 교수님을 찾아가 물었다.\n";
}
 
void print(const char* str, int stk)
{
    for (int j = 0; j < stk; ++j)
    {
        std::cout << "____";
    }
    std::cout << str;
}
 
void solve(int stk)
{
    print("\"재귀함수가 뭔가요?\"\n", stk);
    if (stk == n)
    {
        print("\"재귀함수는 자기 자신을 호출하는 함수라네\"\n", stk);
    }
    else
    {
        print("\"잘 들어보게. 옛날옛날 한 산 꼭대기에 이세상 모든 지식을 통달한 선인이 있었어.\n", stk);
        print("마을 사람들은 모두 그 선인에게 수많은 질문을 했고, 모두 지혜롭게 대답해 주었지.\n", stk);
        print("그의 답은 대부분 옳았다고 하네. 그런데 어느 날, 그 선인에게 한 선비가 찾아와서 물었어.\"\n", stk);
        solve(stk + 1);
    }
    print("라고 답변하였지.\n", stk);
}
 
int main()
{
    input();
    solve(0);
    return 0;
}
cs

 

'알고리즘' 카테고리의 다른 글

백준 14888 : 연산자 끼워넣기  (0) 2020.06.23
백준 14502 : 연구소 (브루트포스)  (0) 2020.06.19
탐색공간의 배제  (0) 2020.06.18
백준 14501 : 퇴사  (0) 2020.06.18
백준 2661 / koi : 좋은 수열  (0) 2020.06.16

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

 

14501번: 퇴사

첫째 줄에 백준이가 얻을 수 있는 최대 이익을 출력한다.

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
#include <iostream>
 
int n;
std::pair<intint> schedule[20];
int ans;
 
void input()
{
    std::cin >> n;
    for (int i = 0; i < n; ++i)
    {
        std::cin >> schedule[i].first >> schedule[i].second;
    }
}
 
void solve(int date, int profit)
{
    if (n < date)
    {
        return;
    }
    if (n == date)
    {
        ans = ans < profit ? profit : ans;
        return;
    }
 
    solve(date + schedule[date].first, profit + schedule[date].second);
    solve(date + 1, profit);
}
 
void output()
{
    std::cout << ans;
}
 
int main()
{
    input();
    solve(00);
    output();
    return 0;
}
cs

 

나도 퇴사시켜줘

 

입사도 못해봤지만

 

 

+ Recent posts