VioletaBabel

2일 : 최대공약수, 최소공배수, 폭탄처리반게임, 스택, 큐 본문

BCA/1. C,C++,C#
2일 : 최대공약수, 최소공배수, 폭탄처리반게임, 스택, 큐
Beabletoet 2018. 2. 6. 09:38

1
2
3
4
5
6
7
8
9
10
#include<iostream>
using namespace std;
int main()
{
    //a와 b를 입력받아 b줄에 a만큼 *을 출력
    int a, b;
    cin >> a >> b;
    for (int i = 0; i++ < b; cout << endl)
        for (int j = 0; j++ < a; cout << "*");
}
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
#include<iostream>
using namespace std;
int main()
{
    //최대공약수, 최소공배수 구하기
    int a, b, x, y;
    cin >> a >> b;
    if (a < b)
    {
        x = b;
        y = a;
    }
    else
    {
        x = a;
        y = b;
    }
    for (int t; x%y != 0; y = t % y)
    {
        t = x;
        x = y;
    }
    cout << "최대공약수 : " << y << "\n최소공배수 : " << a * b / y << endl;
}
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
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
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
    //폭탄처리반 게임 : 10턴 내로 폭탄의 위치를 맞춰라. 가까워질수록 소리가 난다.
    bool fin;
    int c, mx, my, a = 3;
    while (1)
    {
        //오프닝 화면
        srand(time(NULL));
        cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";
        cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";
        cout << "■■■■■□■■■□■□■□■■■□■□□□■■■■■\n";
        cout << "■■■■■□□■□□■□■□□■■□■□■■■■■■■\n";
        cout << "■■■■■□■□■□■□■□■□■□■□□□■■■■■\n";
        cout << "■■■■■□■□■□■□■□■■□□■□■■■■■■■\n";
        cout << "■■■■■□■■■□■□■□■■■□■□■■■■■■■\n";
        cout << "■■■■■□■■■□■□■□■■■□■□□□■■■■■\n";
        cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";
        cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";
        while (1)
        {
            cout << "1. 게임 시작\n2. 게임 종료\n입력 : ";
            cin >> a;
            if (a == 1)
                break;
            else if (a == 2)
                return 1;
            else
                cout << "잘못된 입력입니다!\n";
        }
 
        //필요한 변수들 초기화
        fin = 0;
        c = 0, mx = rand() % 10, my = rand() % 10;
        //cout << mx + 1 << " " << my + 1 << endl; //디버깅용 정답 출력
 
        //게임 시작
        for (int r, x, y; c++ < 10;)
        {
            while (1//x좌표 입력
            {
                cout << c << "번째 시도 (좌표의 범위 : 1~10)\nx좌표 : ";
                cin >> x;
                if (x < 1 || x > 10)
                    cout << "잘못된 입력입니다!\n\n";
                else
                    break;
            }
 
            while (1//y좌표 입력
            {
                cout << "y좌표 : ";
                cin >> y;
                if (y < 1 || y > 10)
                    cout << "잘못된 입력입니다!\n\n";
                else
                    break;
            }
            cout << "\n\n입력된 값 : ( " << x << " , " << y << " )\n탐지기 결과 : ";
 
            //거리 계산 및 신호음 출력
            --x; --y;
            x = (mx > x) ? mx - x : x - mx;
            y = (my > y) ? my - y : y - my;
            for (r = 5 - x - y; r-- > 0cout << "삑")
                if (x+== 0)
                {
                    fin = 1;
                    break;
                }
            cout << "\n\n";
            if (fin)
                break;
        }
 
        //화면 전환
        for (int i = 20; i--;)
            cout << endl;
        if (!fin)
            cout << "땡!!! 정답은 ( " << mx << "," << my << " )" << endl;
        else
            cout << "폭탄을 해체하셨습니다!" << endl;
        cout << "\n게임이 끝났습니다!\n\n";
    }
}
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>
using namespace std;
#define max 10
int main()
{
    //스택 구현
    while (1)
    {
        int num[max] = { 0 }, pos = 0;
        for (int i;1;)
        {
            cout << "1.입력\n2.출력\n3.종료\n : ";
            cin >> i;
            if (i == 1)
            {
                if (pos == max)
                {
                    cout << "스택이 가득 찼습니다.\n\n";
                    continue;
                }
                else
                {
                    cout << "넣을 값을 입력해주세요 : ";
                    cin >> i;
                    num[pos++= i;
                }
            }
            else if (i == 2)
            {
                if (pos == 0)
                {
                    cout << "스택이 비었습니다.\n\n";
                    continue;
                }
                else
                {
                    cout << num[--pos] << endl;
                    num[pos] = 0;
                }
            }
            else if (i == 3)
                return 1;
            else
                cout << "잘못 누르셨습니다.\n\n";
        }
    }
}
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
48
49
50
51
#include<iostream>
using namespace std;
#define max 10
int main()
{
    //큐 구현
    while (1)
    {
        int num[max] = { 0 }, pos1 = 0, pos2 = 0, cnt = 0;
        for (int i;1;)
        {
            cout << "1.입력\n2.출력\n3.종료\n : ";
            cin >> i;
            if (i == 1)
            {
                if (cnt == max)
                {
                    cout << "큐가 가득 찼습니다.\n\n";
                    continue;
                }
                else
                {
                    cout << "넣을 값을 입력해주세요 : ";
                    cin >> i;
                    num[pos1] = i;
                    pos1 = (pos1 + 1) % max;
                    ++cnt;
                }
            }
            else if (i == 2)
            {
                if (cnt == 0)
                {
                    cout << "큐가 비었습니다.\n\n";
                    continue;
                }
                else
                {
                    cout << num[pos2] << endl;
                    num[pos2] = 0;
                    pos2 = (pos2 + 1) % max;
                    --cnt;
                }
            }
            else if (i == 3)
                return 1;
            else
                cout << "잘못 누르셨습니다.\n\n";
        }
    }
}
cs


Comments