반응형

www.acmicpc.net/problem/6064

 

6064번: 카잉 달력

입력 데이터는 표준 입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 입력 데이터의 수를 나타내는 정수 T가 주어진다. 각 테스트 데이터는 한 줄로 구성된다.

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
#include <iostream>
using namespace std;
int t, m, n, x, y;
int gcd(int x, int y) {
    int r;
    while (y != 0) {
        r = x % y;
        x = y;
        y = r;
    }
    return x;
}
int lcm(int x, int y) {
    int g = gcd(x, y);
    return (x / g) * g * (y / g);
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    while (t--) {
        cin >> m >> n >> x >> y;
        int last = lcm(m,n);
        while (1) {
            if (x > last) {
                cout << -1 << '\n';
                break;
            }
            
            if ((x - 1) % n == y - 1) {
                cout << x << '\n';
                break;
            }
            x += m;
        }
    }
}
cs
반응형

'백준 > 브루트포스' 카테고리의 다른 글

백준 14391  (0) 2021.02.15
백준 1182  (0) 2021.02.15
백준 1107 [복습 필수]  (0) 2021.02.13
백준 1476  (0) 2021.02.13
백준 3085  (0) 2021.02.13
반응형

www.acmicpc.net/problem/14500

 

14500번: 테트로미노

폴리오미노란 크기가 1×1인 정사각형을 여러 개 이어서 붙인 도형이며, 다음과 같은 조건을 만족해야 한다. 정사각형은 서로 겹치면 안 된다. 도형은 모두 연결되어 있어야 한다. 정사각형의 변

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
 https://velog.io/@skyepodium/%EB%B0%B1%EC%A4%80-14500-%ED%85%8C%ED%8A%B8%EB%A1%9C%EB%AF%B8%EB%85%B8
#include <iostream>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, m, arr[501][501], val=0, ans = 0;
int dx[] = { 10-10 };
int dy[] = { 010-1 };
int ex[4][4= {
    {000-1}, // ㅗ
    {0121}, // ㅏ
    {0001}, // ㅜ
    {0-101// ㅓ
};
int ey[4][4= {
    {0121}, // ㅗ
    {0001}, // ㅏ
    {0121}, // ㅜ
    {0111// ㅓ
};
bool visited[501][501];
void dfs(int x, int y, int cnt) {
    if (cnt == 4) {
        ans = max(ans, val);
        return;
    }
 
    rep(i, 4) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
            if (!visited[nx][ny]) {
                visited[nx][ny] = 1;
                val += arr[nx][ny];
                dfs(nx, ny, cnt + 1);
                val -= arr[nx][ny];
                visited[nx][ny] = 0;
            }
        }
    }
}
void func2(int x, int y) {
    rep(i, 4) {
    int cnt = 0, temp = 0;
        rep(j, 4) {
            int nx = x + ex[i][j];
            int ny = y + ey[i][j];
            if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
                temp += arr[nx][ny];
                cnt++;
            }
        }
        if (cnt == 4)
            ans = max(ans, temp);
    }
}
 
void func1(int x, int y) {
    int temp = 0;
    // ㅗ
    if (x - 1 >= 0 && x < n && y >= 0 && y + 2 < m) {
        temp = arr[x][y] + arr[x][y + 1+ arr[x - 1][y + 1+ arr[x][y + 2];
        ans = max(ans, temp);
    }
    // ㅏ
    if (x >= 0 && x + 2 < n && y >= 0 && y + 1 < m) {
        temp = arr[x][y] + arr[x+1][y] + arr[x+2][y] + arr[x+1][y + 1];
        ans = max(ans, temp);
    }
    // ㅜ
    if (x >= 0 && x + 1 < n && y >= 0 && y + 2 < m) {
        temp = arr[x][y] + arr[x][y + 1+ arr[x +1][y + 1+ arr[x][y + 2];
        ans = max(ans, temp);
    }
    // ㅓ
    if (x - 1 >= 0 && x + 1 < n && y >= 0 && y + 1 < m) {
        temp = arr[x][y] + arr[x][y + 1+ arr[x - 1][y + 1+ arr[x+1][y + 1];
        ans = max(ans, temp);
    }
 
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    rep(i, n) {
        rep(j, m) {
            cin >> arr[i][j];
        }
    }
 
    rep(i, n) {
        rep(j, m) {
            if (!visited[i][j]) {
                visited[i][j] = 1;
                val += arr[i][j];
                dfs(i, j, 1);
                func2(i, j);
                val -= arr[i][j];
                visited[i][j] = 0;
            }
        }
    }
    cout << ans;
}
cs
반응형

'백준 > 삼성기출' 카테고리의 다른 글

백준 14499  (0) 2021.02.21
백준 14501  (0) 2021.02.15
백준 15686  (0) 2021.02.05
백준 14502  (0) 2021.02.03
백준 15684  (0) 2021.02.03
반응형

www.acmicpc.net/problem/1107

 

1107번: 리모컨

첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다.  둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼

www.acmicpc.net

반례 모음 www.acmicpc.net/board/view/46120

 

글 읽기 - [반례모음]

댓글을 작성하려면 로그인해야 합니다.

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
#include <iostream>
#include <algorithm>
#include <cstring>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, m, now = 100, ans = 987654321;
int button[11];
void func(int num, int cnt) {
    // main에서 처음 넘겨주는 num이 0이므로
    // 숫자를 하나도 추가하지 않았을 때(cnt==0)는 검사 안함
    // 처리 안해주면 반례 발생
    if(cnt > 0)
        ans = min(ans, abs(n-num) + cnt);
    
    // 종료 조건
    if (cnt == 6) {
        return;
    }
    // 백트래킹으로 숫자 증가
    for (int i = 0; i < 10; i++) {
        if (button[i]) {
            num = num * 10 + i;
            func(num, cnt +1);
            num = num - i;
            num /= 10;
        }
    }
}
int main() {
    memset(button, 1sizeof(button));
    cin >> n >> m;
    rep(i, m) {
        int x;
        cin >> x;
        button[x] = 0;
    }
    ans = abs(n - 100);
    
    func(00);
    cout << ans;
}
cs
반응형

'백준 > 브루트포스' 카테고리의 다른 글

백준 1182  (0) 2021.02.15
백준 6064  (0) 2021.02.13
백준 1476  (0) 2021.02.13
백준 3085  (0) 2021.02.13
백준 1436  (0) 2021.01.25
반응형

www.acmicpc.net/problem/1476

 

1476번: 날짜 계산

준규가 사는 나라는 우리가 사용하는 연도와 다른 방식을 이용한다. 준규가 사는 나라에서는 수 3개를 이용해서 연도를 나타낸다. 각각의 수는 지구, 태양, 그리고 달을 나타낸다. 지구를 나타

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int a, b, c;
int main() {
    cin >> a >> b >> c;
    if (a == b == c) {
        cout << a;
        return 0;
    }
    int i = 1;
    while (1) {
        if ((i % 15 + 1== a && (i % 28 + 1== b && (i % 19 + 1== c) {
            cout << i+1;
            break;
        }
        i++;
    }
}
cs
반응형

'백준 > 브루트포스' 카테고리의 다른 글

백준 6064  (0) 2021.02.13
백준 1107 [복습 필수]  (0) 2021.02.13
백준 3085  (0) 2021.02.13
백준 1436  (0) 2021.01.25
백준 1018  (0) 2021.01.25
반응형

www.acmicpc.net/problem/3085

 

3085번: 사탕 게임

첫째 줄에 상근이가 먹을 수 있는 사탕의 최대 개수를 출력한다.

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
#include <iostream>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n;
char arr[51][51];
void swap(int& a, int& b) {
    int tmp = a;
    a = b;
    b = tmp;
}
int chk(int i, int j) {
    int hor = 0, ver = 0, x, y;
    char c = arr[i][j];
    // 가로 확인
    // 좌
    x = i;
    y = j;
    while (y - 1 >= 0 && arr[x][y - 1== c) {
        hor++;
        y--;
    }
    // 우
    x = i;
    y = j;while (y + 1 < n && arr[x][y + 1== c) {
        hor++;
        y++;
    }
    //세로 확인
    // 상
    x = i;
    y = j;while (x - 1 >= 0 && arr[x-1][y] == c) {
        ver++;
        x--;
    }
    // 하
    x = i;
    y = j;
    while (x + 1 < n  && arr[x+1][y] == c) {
        ver++;
        x++;
    }
    return max(hor, ver);
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    rep(i, n) {
        rep(j, n)
            cin >> arr[i][j];
    }
    int ans = -1, ret;
    rep(i, n) {
        rep(j, n) {
            if (j + 1 < n) {
                swap(arr[i][j], arr[i][j + 1]);
                ret = chk(i, j);
                ans = max(ans, ret);
                ret = chk(i, j+1);
                ans = max(ans, ret);
                swap(arr[i][j], arr[i][j + 1]);    // roll back
            }
            if (i + 1 < n) {
                swap(arr[i][j], arr[i + 1][j]);
                ret = chk(i, j);
                ans = max(ans, ret);
                ret = chk(i+1, j);
                ans = max(ans, ret);
                swap(arr[i][j], arr[i + 1][j]);    // roll back
            }
        }
    }
    cout << ans + 1;
}
cs
반응형

'백준 > 브루트포스' 카테고리의 다른 글

백준 6064  (0) 2021.02.13
백준 1107 [복습 필수]  (0) 2021.02.13
백준 1476  (0) 2021.02.13
백준 1436  (0) 2021.01.25
백준 1018  (0) 2021.01.25
반응형

www.acmicpc.net/problem/6588

 

6588번: 골드바흐의 추측

각 테스트 케이스에 대해서, n = a + b 형태로 출력한다. 이때, a와 b는 홀수 소수이다. 숫자와 연산자는 공백 하나로 구분되어져 있다. 만약, n을 만들 수 있는 방법이 여러 가지라면, b-a가 가장 큰

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
#include <iostream>
#include <vector>
using namespace std;
bool not_prime[1000001];
vector<int> v;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // 소수가 아닌 숫자 찾기
    not_prime[1= 1;
    for (int i = 2;i <= 1000000;i++) {
        if (not_prime[i]) continue;
        for (int j = 2;i * j <= 1000000;j++
            not_prime[i * j] = 1;
    }
    // 모든 소수를 벡터에 저장
    for (int i = 3;i <= 1000000;i++) {
        if (!not_prime[i])
            v.push_back(i);
    }
    int n;
    cin >> n;
    while (n != 0) {
        for (int i : v) {
            if (!not_prime[n - i]) {
                cout << n << " = " << i << " + " << n - i << '\n';
                break;
 
            }
        }
        cin >> n;
    }
    
}
cs
반응형
반응형

www.acmicpc.net/problem/1978

 

1978번: 소수 찾기

첫 줄에 수의 개수 N이 주어진다. N은 100이하이다. 다음으로 N개의 수가 주어지는데 수는 1,000 이하의 자연수이다.

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
#include <iostream>
using namespace std;
int n, x, cnt = 0;
bool not_prime[1001];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    // 에라토스테네스의 체
    not_prime[1= 1;
    for (int i = 2; i <= 1000; i++) {
        if (not_prime[i]) continue;
        for (int j = 2; i * j <= 1000; j++)
            not_prime[i * j] = 1;
    }
 
    cin >> n;
    while (n--) {
        cin >> x;
        if (!not_prime[x])
            cnt++;
    }
    cout << cnt;
}
cs
반응형
반응형

www.acmicpc.net/problem/2609

 

2609번: 최대공약수와 최소공배수

첫째 줄에는 입력으로 주어진 두 수의 최대공약수를, 둘째 줄에는 입력으로 주어진 두 수의 최소 공배수를 출력한다.

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
#include <iostream>
#include <algorithm>
using namespace std;
// 유클리드 호제법
int gcd(int a, int b) {
    int x = max(a, b);
    int y = min(a, b);
    int r;
    while (y > 0) {
        r = x % y;
        x = y;
        y = r;
    }
    return x;
}
int lcm(int a, int b) {
    int common = gcd(a, b);
    return (a / common) * common * (b / common);
}
int main() {
    int a, b;
    cin >> a >> b;
    cout << gcd(a, b) << '\n'
        << lcm(a, b);
}
cs
반응형

+ Recent posts