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
#include <iostream>
void main(){
    int a[10]={0},input,i=0,j=9, count=1;
 
    while(1) {
        std::cout<<count<<"입력: ";
        std::cin>>input;
        if (input == 0)
            break;
        if (input%2 == 1) {
            a[i]=input;//홀수
            for(int i=0; i<=9; i++)
                std::cout<<a[i];
            i++;
            std::cout<<"\n";
        }
        else {
            a[j]=input;//짝수
            for(int i=0; i<=9; i++)
                std::cout<<a[i];
            j--;
            std::cout<<"\n";
        }
        count++;
        if (a[5!= 0)
            break;
    }
    for(int i=0; i<=9; i++)
        std::cout<<"a["<<i<<"]="<<a[i]<<"\n";
}
cs

윈도우 프로그래밍 실습


사용자에게 입력받아 홀수면 왼쪽, 짝수면 오른쪽에 저장한다


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


사용자로부터 두개의 숫자를 입력받아 구구단을 출력하는 프로그램을 만들어 보자. 


예를 들어 사용자가 3과 5를 입력하면, 3단, 4단, 5단을 출력해야 한다.


단 사용자의 입력 순서가 자유로워야 한다. 3과 5를 입력하던 5와 3을 입력하던 같은 결과를 출력해야 한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
 
int main() {
    int input1, input2, temp = 0;
 
    std::cout << "두 개의 숫자 입력: ";
    std::cin >> input1 >> input2;
 
    if (input1 > input2) {
        input2 = temp;
        input2 = input1;
        input1 = temp;
    }
 
    for (; input1 <= input2; input1++) {
        for (int i = 1; i <= 9; i++) {
            std::cout << input1 << " X " << i << " = " << input1*<< "\n";
        }
        std::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
#include <iostream>
#include <time.h> //time 사용을 위한 헤더파일
 
int main(int argc, const char * argv[]) {
    int human, computer, option, totalgame = 0, hw = 0, cw = 0, hl = 0, cl = 0, hh = 0, cc = 0;
    srand(time(NULL)); //매번 다른 난수 발생을 위해 시드값 사용 *time을 이용해 시간을 시드값으로 사용
    do { // 게임을 한번 시작 후 종료 트리거 실행을 위해 do while문 사용
        option = 0// 2,147,483,648 이상 입력시 무한루프에 빠지는데 0 초기화값을 주면 루프돌지 않고 종료됨
        std::cout << "계속 할꺼면 1 입력 (0 입력시 종료)";
        std::cin >> option; // 0 입력시 종료하고 그 외 숫자, 문자 입력시 게임 시작
        human = rand() % 6 + 1// 인간 1부터 6까지 난수 발생 후 저장
        computer = rand() % 6 + 1// 컴퓨터 1부터 6까지 난수 발생 후 저장
        if (human>computer) {
            std::cout << human << ":" << computer << " 이세돌 승\n";
            hw += 1, cl += 1// hw - 인간 승, cl - 컴퓨터 배패
        }
        else if (computer>human) {
            std::cout << human << ":" << computer << " 알파고 승\n";
            cw += 1, hl += 1// cw - 컴퓨터 승, hl - 인간 패배
        }
        else {
            std::cout << human << ":" << computer << " 무승부\n";
            hh += 1, cc += 1// hh, cc - 무승부
        }
        totalgame += 1// 총 게임수 계산
    } while (option != 0); // 0 입력시 종료
    std::cout.precision(3); // 승률 소수점 세자리 고정 
    std::cout << "\n이세돌 승:" << hw << " 패:" << hl << " 무:" << hh << " 승률:" << (double)hw / (hl + hh) << "\n";
    std::cout << "\n알파고 승:" << cw << " 패:" << cl << " 무:" << cc << " 승률:" << (double)cw / (cl + cc) << "\n";
    std::cout << "\n총 " << totalgame << "판함\n\n";
    return 0;
}
cs


열심히 주석을 달아놨으니 이해가 안되시면 참고하세요...


주사위 난수출력을 위해 rand() 함수를 사용하는데 매번 다른 난수 출력을 위해선 srand() 함수를 추가해야 합니다.


만약 srand() 함수를 사용하지 않는다면 프로그램 종료 후 재실행시 매번 같은 난수가 출력됩니다.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
 
int main(){
    int input;
    
    std::cout<<"10진수 정수 입력: ";
    std::cin>>input;
    
    std::cout<<"8진수: "<< std::oct <<input<<"\n";
    std::cout<<"10진수: "<< std::dec <<input<<"\n";
    std::cout<<"16진수: "<< std::hex <<input<<"\n";
}
cs


이런식으로 출력할 변수 앞에 std::oct, std::dec, std::hex 를 붙여주면 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
 
int main(){
    int a,result=0;
    
    while(1){
        std::cout<<"입력: ";
        std::cin>>a;
        
        result=a;
        
        for(int i=1; i<a; i++) {
            if (i == a)
                break;
            result*=(a-i);
        }
        std::cout<<result<<"\n";
    }
}
cs


+ Recent posts