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
#include <iostream>
 
using namespace std;
 
int main() {
    int i, length, count = 0;
    char c[100];
 
    cin >> c;
 
    for (i = 0; c[i] != NULL; i++); // 길이 구하기
 
    length = i;
 
    for (i = 0; i < length; i++)
        if (c[i] != c[length - 1 - i])
            count++;
 
    if (count > 0 || length < 3) {
        cout << "화문이 아닙니다.\n";
        return 0;
    }
 
    else
        cout << "화문 입니다.\n";
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
 
using namespace std;
 
int main() {
    int a;
 
    cout << "10진수 입력:";
    cin >> a;
 
    for (int i = 7; i >= 0; i--
        cout<<(a >> i & 1 ? 1 : 0);
}
cs


'C++ > 알고리즘' 카테고리의 다른 글

c++ 포인터 call by reference 콜 바이 레퍼런스  (0) 2016.04.06
c++ 알고리즘 회문 체크  (0) 2016.04.02
c++ 홀수 왼쪽 짝수 오른쪽  (0) 2016.04.02
c++ 알고리즘 텍스트 개수 세기  (0) 2016.03.31
c++ 숫자야구  (0) 2016.03.29
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>
 
using namespace std;
 
void hs(int *hs);
void js(int *js);
 
int main() {
    int a[10];
 
    for (int i = 0; i < 10; i++) {
        cout << i << "번 째 입력:";
        cin >> a[i];
    }
 
    hs(a);
    js(a);
 
void hs(int *hs) {
    for (int i = 0; i < 10; i++) {
        if (*(hs + i) % 2 == 1)
            cout << *(hs + i) << ",";
    }
    cout << endl;
}
 
void js(int *js) {
    for (int i = 0; i < 10; i++) {
        if (*(js + i) % 2 == 0)
            cout << *(js + i) << ",";
    }
    cout << endl;
}
cs




1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
 
int main(){
    int count=0;
    char *a= "text @ ";
    for(int i=0*(a+i) !=NULL; i++) {
        if (*(a+i) != NULL && *(a+i) != ' ') count++;
        if (32 <= *(a+i) => 64) count++;
    }
    std::cout<<count<<"\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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <time.h>
 
void intro(); // 스플래시 
void com_mkrand(); // 컴퓨터 난수 생성,저장
int human_input(); // 플레이어 숫자 입력, 난수와 비교
int Avoid_duplication(); // 중복 숫자 입력 체크
int Avoid_range(); // 인풋 범위 체크
void count_reset(); // 카운트 리셋
 
int output[4], input[4], strike = 0, ball = 0, out = 0, count = 0// 리턴을 주는것보다 전역함수로 제어하는게 효율적이라고 판단
 
int main() {
    int exit;
    srand((unsigned)time(NULL)); // 매번 다른 난수 발생 위해 시드값 부여
    intro(); // 스플래시
 
    com_mkrand(); // 난수 발생
 
    exit = human_input(); // 플레이어 숫자 입력
    if (exit == 0// -1 입력시 종료
        return 0;
 
    std::cout << "\n                  " << count << "\a회 만에 당신이 이겼습니다! \n" << "                  정답 - ";
 
    for (int i = 0; i < 4; i++)
        std::cout << output[i] << " ";
    std::cout << "\n\n";
}
 
void com_mkrand() {
    output[0= rand() % 10// 첫번째 자리0부터 9까지 난수 생성
    while (1) {
        output[1= rand() % 10// 첫번째 자리와 중복되지 않는 난수 발생할때 까지 반복
        if (output[1!= output[0])
            break;
    }
    while (1) { // 왼쪽에 위치한 숫자들과 중복되지 않는 난수 발생할때 까지 반복
        output[2= rand() % 10;
        if (output[2!= output[0] && output[2!= output[1])
            break;
    }
    while (1) { // 왼쪽에 위치한 숫자들과 중복되지 않는 난수 발생할때 까지 반복
        output[3= rand() % 10;
        if (output[3!= output[0] && output[3!= output[1] && output[3!= output[2])
            break;
    }
    std::cout.width(20);
    for (int i = 0; i < 4; i++)
        std::cout << output[i] << " ";
    std::cout << "\n";
}
 
int human_input() {
    int num; // 중복숫자입력 체크용
    while (strike < 4) {
        count++// 게임 횟수 체크
        count_reset();
        std::cout << count << "회 플레이어 숫자 입력 (스페이스 바로 자릿수 입력 -1 입력시 종료):";
        std::cin >> input[0];
        if (input[0== -1// 종료 플래그
            return 0;
        std::cin >> input[1>> input[2>> input[3];
 
        num = Avoid_duplication(); // 중복, 범위 검사
 
        if (num == 1) {
        case1:
            std::cout << count << "회 플레이어 10보다 작은 중복 되지 않는 숫자 입력 (스페이스 바로 자릿수 입력 -1 입력시 종료):";
            std::cin >> input[0];
            if (input[0== -1// 종료 플래그
                return 0;
            std::cin >> input[1>> input[2>> input[3];
            num = Avoid_duplication(); // 중복, 범위 재검사
            if (num == 1// 검사 통과시 num 0반환, 못할 시 1반환. num 1반환 시 다시 입력
                goto case1;
        }
 
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (input[i] == output[i]) {
                    strike++// 스트라이크 카운트
                    break;
                }
                if (input[i] == output[j]) {
                    ball++// 볼 카운트
                    break;
                }
            }
        }
        out = 4 - strike - ball; // 아웃 카운트
        std::cout << "-----------------------------------------------------------------\n";
        std::cout.width(19);
        std::cout << strike << " 스트라이크! " << ball << " 볼! " << out << " 아웃!\n";
        std::cout << "-----------------------------------------------------------------\n";
    }
}
 
int Avoid_duplication() { // 중복 숫자 검사 함수
    int num;
    count_reset();
    for (int i = 0, j = 1; i < 3; i++) { // 중복 숫자 검사
        j = i + 1;
        for (; j < 4; j++) {
            if (input[i] == input[j]) {
                ball++;
            }
        }
    }
 
    if (ball > 0) { // 중복 시 1 반환
        count_reset();
        return 1;
    }
    else { // 중복이 아닐시
        count_reset();
        ball = Avoid_range(); // 10보다 큰 수가 있는지 확인 후
        if (ball > 0)
            return 1// 있을 시 1 반환
        else
            return 0// 없을 시 0 반환
    }
}
 
void count_reset() {
    strike = 0; ball = 0; out = 0// 카운트 리셋 함수
}
 
int Avoid_range() { // 범위 검사 함수
    count_reset();
    for (int i = 0; i < 4; i++) { // 사용자 input이 10보다 큰 수가 있는지 검사
        if (input[i] >= 10)
            ball++// 있을 시 ball 카운트
    }
    if (ball > 0) { // 10보다 큰 수 있을 시 1 반환
        count_reset();
        return 1;
    }
    else { // 없으면  0반환
        count_reset();
        return 0;
    }
}
 
void intro() { // 스플래시
    std::cout << "┌──────────────────────────────────┐\n";
    std::cout << "│                           숫자야구                                 │\n";
    std::cout << "│        윈도우 프로그래밍           김종완 교수님                   │\n";
    std::cout << "│                                                                    │\n";
    std::cout << "│                             developed by - 고상욱(20130889)        │\n";
    std::cout << "└──────────────────────────────────┘\n";
    std::cout.width(24);
    std::cout << "Wait";
    std::cout << ".";
    std::cout << ".";
    std::cout << ".\n";
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
 
int main() {
    char input[100], temp=0;
    while(1) {
        cout << "입력:";
        cin >> input;
 
        for (int i = 0; input[i] != NULL; i++) {
            temp = (temp < input[i]) ? input[i] : temp;
        }
 
        cout << temp<<"\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
#include <iostream>
using namespace std;
 
int main() {
    int a=0;
    char num[100], temp[4];
 
    cout << "문자열 입력:";
    cin >> num;
 
    for (int i = 0; num[i]!=0; i++) {
        a++;
    }
    a -= 1;
    cout << num[a]<<"\n";
 
    for (int i = 0; a > 0; a--,i++) {
        if (a - i == 1 || a==i)
            break;
        * temp = num[a];
        num[a] = num[i];
        num[i] = * temp;
    }
    cout << num;
}
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
#include <iostream>
#include <time.h>
#include <Windows.h>
 
void main(){
    int a[50]={0},input,i=0,j=49, count=0;
    srand(time(NULL));
    while(1) {
        input=rand() % 9 + 1;
        if (input%2 == 1) {
            a[i]=input;//홀수
            for(int i=0; i<=49; i++)
                std::cout<<a[i];
            i++;
            std::cout<<"\n";
        }
        else {
            a[j]=input;//짝수
            for(int i=0; i<=49; i++)
                std::cout<<a[i];
            j--;
            std::cout<<"\n";
        }
        count++;
        if (count==49) {
            for(int i=0; i<=49; i++
                a[i]=0;
            i=0,j=49;
            count=0;
        }
        Sleep(50);
    }    
}
cs


윈도우 프로그래밍 실습


난수를 발생시켜 홀수면 왼쪽, 짝수면 오른쪽에 저장한다


난수 50개가 전부 차면 0으로 초기화 한다

+ Recent posts