문제 풀이

 

 에라토스테네스의 체를 활용하여 소수를 구했습니다.

(https://ko.wikipedia.org/wiki/%EC%97%90%EB%9D%BC%ED%86%A0%EC%8A%A4%ED%85%8C%EB%84%A4%EC%8A%A4%EC%9D%98_%EC%B2%B4)

 

 

코드

 

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>
 
using namespace std;
 
int d[1001= { 0, };
 
int main() {
 
    int n;
    int m = 0;
    cin >> n;
 
    vector <int> v;    
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        if(x > m)
            m = x;
        v.push_back(x);
    }
    
    d[1= 1;
    for (int i = 2; i <= m; i++)
    {
        if (d[i] == 0)
        {
            for (int j = i; j*<= m; j++)
            {
                if (d[j*i] == 0)
                    d[j*i] = 1;
            }
        }
    }
 
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        if (d[v[i]] == 0)
            ans++;
    }
 
    cout << ans;
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

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

 

1978번: 소수 찾기

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

www.acmicpc.net

 

문제 해설

 

 자료형을 연습하기 좋은 문제라고 생각이 들어 가져오게 되었습니다. 유의해야 할 점은 총 두 가지로 반올림과 최빈값 구하기입니다.

 반올림 같은 경우, 양수와 음수일 때를 고려하여 양수일 때는 0.5를 더해준 값을 int형으로 변환시켜 저장해 주었고 음수일 때는 0.5를 빼준 값을 int형으로 변환시켜 저장하였습니다. 최빈값의 경우, 우선 가장 빈도수가 높을 때 빈도가 몇인지 구하여 같은 빈도가 있는지 확인해 주었습니다.

 

 

코드

 

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
#include <cstdio>
#include <algorithm>
#include <vector>
 
using namespace std;
 
int d[500000];
 
int main() {
 
    int n;
    int bi[8001= { 0, };
    int sum = 0, bm = 0, cm = 0, dm = 0;
 
    scanf("%d"&n);
 
    for (int i = 0; i < n; i++)
    {
        scanf("%d"&d[i]);
        sum += d[i];
 
        int x = d[i] + 4000;
        bi[x]++;
    }
 
    sort(d, d + n);
 
    int maximum = 0;
    for (int i = 0; i <= d[n - 1+ 4000; i++)
    {
        if (bi[i] > maximum)
            maximum = bi[i];
    }
 
    vector <int> v;
    for (int i = 0; i <= d[n - 1+ 4000; i++)
    {
        if (bi[i] == maximum)
            v.push_back(i);
    }
 
    float am = float(sum) / float(n);
 
    if (am >= 0)
        am = int(am + 0.5);
    else
        am = int(am - 0.5);
 
    bm = d[n / 2];
 
    if (v.size() > 1)
        cm = v[1- 4000;
    else
        cm = v[0- 4000;
 
    dm = d[n - 1- d[0];
 
    printf("%d\n"int(am));
    printf("%d\n", bm);
    printf("%d\n", cm);
    printf("%d\n", dm);
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

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

 

2108번: 통계학

첫째 줄에 수의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 그 다음 N개의 줄에는 정수들이 주어진다. 입력되는 정수의 절댓값은 4,000을 넘지 않는다.

www.acmicpc.net

 

문제 풀이

 

 게임의 규칙을 찾아야 풀이가 가능한 문제입니다. 게임의 규칙은 다음과 같습니다.

 

1. 같은 곳을 두 번 뒤집으면 결과가 같으므로 이러한 경우는 고려하지 않아도 됩니다.

2. N은 짝수이므로,

2-1) (x, y)의 돌 (=노란색 별) 을 뒤집으면 자기 자신과 가로줄, 세로줄의 돌 색이 바뀌게 되므로 노란색 별 기준 주변의 검은색 돌 갯수는 홀수개가 됩니다.

2-2) 초록색 별 기준 바뀌는 돌의 색은 자기 자신과 자기 자신이 속한 한 줄이 되므로 검은 돌의 갯수는 짝수개가 됩니다.

2-3) 자기 자신의 색이 바뀌지 않는 빨간색 별 기준 바뀌는 돌의 색은 가로 줄 한 개, 세로 줄 한 개이므로 검은 돌의 갯수는 홀수개가 됩니다.

(+ 흰 색 돌을 기준으로 보는 경우 뒤집은 돌의 주변 흰색 돌만 짝수개가 됩니다.)

3. 즉, 가로줄의 검은 돌 갯수와 세로 줄의 검은 돌 갯수를 합한 것이 홀수라면, 뒤집은 돌이 됩니다. (중복 제외)

(+ cin, cout으로 받으면 시간 초과납니다ㅠㅠ)

 

 

코드

 

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
#include <iostream>
 
using namespace std;
 
int main() {
 
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    int t;
    cin >> t;
 
    for (int tc = 1; tc <= t; tc++)
    {
        int ans = 0;
        char board[1000][1000];
        int h[1000= { 0, };
        int v[1000= { 0, };
 
        int n;
        cin >> n;
 
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cin >> board[i][j];
                if (board[i][j] == 'B')
                {
                    h[i]++;
                    v[j]++;
                }
            }
        }
 
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                int x = h[i] + v[j];
                if (board[i][j] == 'B')
                    x -= 1;
 
                if (x % 2 != 0)
                    ans++;
            }
        }
        cout << '#' << tc << ' ' << ans << '\n';
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWefzFeq5P8DFAUh&categoryId=AWefzFeq5P8DFAUh&categoryType=CODE

 

SW Expert Academy

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

swexpertacademy.com

 

문제 풀이

 

 bfs 알고리즘을 활용하여 방문하지 않은 길을 따라 도착점이 나올 때까지 탐색하였습니다.

 

 

코드

 

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
#include <cstdio>
#include <queue>
 
using namespace std;
 
int ans;
int map[16][16];
bool isVisit[16][16];
int dx[] = { 00-11 };
int dy[] = { -1100 };
 
void bfs(int a, int b)
{
    queue <pair<intint>> q;
    q.push(make_pair(a, b));
    isVisit[a][b] = true;
 
    while (!q.empty())
    {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx >= 0 && ny >= 0 && nx < 16 && ny < 16)
            {
                //도착점이라면 ans = 1로 하고, 리턴
                if (map[nx][ny] == 3)
                {
                    ans = 1;
                    return;
                }
                //방문한 적 없는 길인 경우
                else if (map[nx][ny] == 0 && isVisit[nx][ny] == false)
                {
                    isVisit[nx][ny] = true;
                    q.push(make_pair(nx, ny));
                }
            }
        }
    }
}
 
int main() {
 
    while(1)
    {
        int tc;
        scanf("%d"&tc);
        ans = 0;
        int x = 0, y = 0//starting point;
        for (int i = 0; i < 16; i++)
        {
            for (int j = 0; j < 16; j++)
            {
                scanf("%1d"&map[i][j]);
 
                if (map[i][j] == 1)
                    isVisit[i][j] = true;
 
                else if (map[i][j] == 2)
                {
                    x = i;
                    y = j;
                    isVisit[i][j] = false;
                }
 
                else
                    isVisit[i][j] = false;
            }
        }
 
        bfs(x, y);
 
        printf("#%d %d\n", tc, ans);
        
        if (tc == 10)
            break;
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14vXUqAGMCFAYD&categoryId=AV14vXUqAGMCFAYD&categoryType=CODE&&&

 

SW Expert Academy

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

swexpertacademy.com

 

문제 풀이

 

 다양한 방법으로 풀이가 가능한 문제로 저는 포인터를 역 방향으로 참조하여 문제를 풀었습니다.

 

 

코드

 

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
    string d;
    cin >> d;
    sort(d.rbegin(), d.rend());
    cout << d;
}

 

 

제출 결과

 

 

 

문제 출처

 

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

 

1427번: 소트인사이드

첫째 줄에 정렬하고자하는 수 N이 주어진다. N은 1,000,000,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
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    int n;
    int d[1000];
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> d[i];
 
    //bubble sort
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n - i - 1; j++)
        {
            if (d[j] > d[j + 1])
            {
                int tmp = d[j];
                d[j] = d[j + 1];
                d[j + 1= tmp;
            }
        }
    }
 
    for (int i = 0; i < n; i++)
        cout << d[i] << '\n';
 
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

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

 

2750번: 수 정렬하기

첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄부터 N개의 줄에는 숫자가 주어진다. 이 수는 절댓값이 1,000보다 작거나 같은 정수이다. 수는 중복되지 않는다.

www.acmicpc.net

 

풀이 과정

 

 arr에 map을 입력 받고, 시작점인 (0, 0)부터 탐색을 시작합니다. 해당 칸까지 이동하였을 때, 걸린 최소 비용을 dist 배열에 저장해 줍니다. dist 배열은 매우 큰 수로 초기값을 정해 줍니다. 

 

 간단한 예를 들어, dist에 파란색 경로의 최소 비용이 저장되어 있을 때, 주황색 경로의 최소 비용이 더 작은 경우 dist에 저장된 값을 주황색 경로의 값으로 갱신하여 줍니다. 따라서 탐색이 종료되면, dist[n-1][n-1]에는 (n-1, n-1)까지 가는 최소 비용이 저장되어 있게 됩니다.

 

 

코드

 

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
#include <cstdio>
#include <queue>
using namespace std;
 
int n;
int arr[100][100], dist[100][100];
int dx[] = { 00-11 };
int dy[] = { -1100 };
 
void search()
{
    queue <pair<intint>> q;
    q.push(make_pair(00));
 
    while (!q.empty())
    {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];
 
            if (nx < n && ny < n && nx >= 0 && ny >= 0)
            {
                int ndist = arr[nx][ny] + dist[x][y];
                if (ndist < dist[nx][ny])
                {
                    q.push(make_pair(nx, ny));
                    dist[nx][ny] = ndist;
                }
            }
        }
    }
    return;
}
 
int main(){
 
    int t;
    scanf("%d"&t);
 
    for (int tc = 1; tc <= t; tc++)
    {
        scanf("%d"&n);
 
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
            {
                scanf("%1d"&arr[i][j]);
                dist[i][j] = 99999999;
            }
 
        dist[0][0= 0;
        search();
 
        printf("#%d %d\n", tc, dist[n-1][n-1]);
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV15QRX6APsCFAYD&categoryId=AV15QRX6APsCFAYD&categoryType=CODE&&&

 

SW Expert Academy

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

swexpertacademy.com

 

문제 풀이

 

 이 문제는 단순히 구현을 했을 때는 시간 초과가 나게 됩니다. 따라서 x를 고정시켜 주고, 이 x에 따라 나올 수 있는 y를 찾아가면서 문제를 풀어야 합니다.

 

 예를 들어, 10 12 3 9의 경우 3이 x의 자리에 올 때 y의 자리에 올 수 있는 값은 3, 1, 11, 9, ... 이며 이 때 각각 10년의 텀을 두고 발생하게 됩니다. 따라서 답은 3년에 3*10을 더한 33년이 됩니다. 주의할 점은 % 연산의 경우 0부터 시작하기 때문에 이를 고려하여 코드를 구성해야 합니다.

 

 만약 가장 처음에 y 자리에 왔던 값이 다시 나타나게 된다면, 이는 한 주기를 모두 지났다는 뜻이 되므로 종말 년도를 넘어섰다는 뜻이 됩니다. 따라서 -1을 출력합니다.

 

 또는 최소 공배수와 최대 공약수를 활용하여 종말 년도를 먼저 계산하고, 이를 넘어서는 경우를 찾아주는 것도 가능합니다.

 

 

코드

 

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
#include <iostream>
using namespace std;
 
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    int t;
    cin >> t;
 
    while (t--)
    {
        int m, n, x, y;
        cin >> m >> n >> x >> y;
 
        int ans = x, b = (x - 1) % n + 1;
        int tmp = b;
 
        while (1)
        {
            if (b == y)
                break;
            
            b = (b + m - 1) % n + 1
            ans += m;
 
            if (tmp == b)
            {
                ans = -1;
                break;
            }
        }
 
        cout << ans << "\n";
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

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

 

6064번: 카잉 달력

문제 최근에 ICPC 탐사대는 남아메리카의 잉카 제국이 놀라운 문명을 지닌 카잉 제국을 토대로 하여 세워졌다는 사실을 발견했다. 카잉 제국의 백성들은 특이한 달력을 사용한 것으로 알려져 있다. 그들은 M과 N보다 작거나 같은 두 개의 자연수 x, y를 가지고 각 년도를 와 같은 형식으로 표현하였다. 그들은 이 세상의 시초에 해당하는 첫 번째 해를 <1:1>로 표현하고, 두 번째 해를 <2:2>로 표현하였다. 의 다음 해를 표현한 것을 이라고 하자. 만일

www.acmicpc.net

 

+ Recent posts