본문 바로가기
C++

C++ 기초 예제 풀이

by ADELA_J 2023. 5. 19.

Q. 2개의 숫자를 입력해 A가 B의 배수인지 아닌지 출력하라.

 

int main() {
	int a, b;
	cout << "2개의 숫자를 입력하시오.";
	cin >> a >> b;
	if (a % b == 0)
		cout << "A는 B의 배수이다.";
	else
		cout << "A는 B의 배수가 아니다.";

2. 글자를 입력해 글자 속에 a(A)가 몇 개 있는지 출력하라

int main() {
    char word[100];
    cout << "A가 몇개 있는지 궁금한 단어를 작성하세요:";
    cin.getline(word, 100, '\n');
    int a = 0;
    for (int n = 0; n < 100; n++) {
        if (word[n] == 'A' || word[n] == 'a') {
            a += 1;
        }
    }
    cout << a;
}

 

3. 숫자 3개를 차례로 입력해 가장 큰 수와 작은 수를 출력해라.

int main() {
	int a, b, c;
	cout << "숫자 3개를 차례로 입력하세요 >> ";
	cin >> a >> b >> c;

	if (a > b) {
		if (a > c) {
			cout << "가장 큰 수" << a << '\n';
			if (b < c) {
				cout << "가장 작은 수" << b;
			}
			else {
				cout << "가장 작은 수" << c;
			}
		}
		if (a < c) {
			cout << "가장 큰 수" << c << '\n';
			if (a < b) {
				cout << "가장 작은 수 " << a;
			}
			else {
				cout << "가장 작은 수" << b;
			}
		}
	}
	else {
		if (b > c) {
			cout << "가장 큰 수 " << b << '\n';
			if (c > a) {
				cout << "가장 작은 수 " << a;
			}
			else {
				cout << "가장 작은 수" << c;
			}
		}
		else {
			cout << "가장 큰 수" << c << '\n';
			if (a > b) {
				cout << "가장 작은 수" << b;
			}
			else {
				cout << "가장 작은 수" << a;
			}
		}
	}
}

3-2. 더 짧게 짜보기,,,,

 

 

4. 문자열을 하나 입력 받고 문자열의 부분을 하나하나 글자를 출력하는 프로그램을 짜보시오.

 

int main() {
	char sentence[100];

	cout << "문자열을 입력하세요 : ";
	cin.getline(sentence, 100);

	int str_len = strlen(sentence);

	string output = "";

	for (int i = 0; i < str_len; i++) {
		for (int j = str_len ; j < i; j++) {
			cout << sentence[j];
		}
		cout << "\n";
	}
}

 

 

5. 구구단 짜기

int main() {
    int a, b;

    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= 9; j++) {
            cout << j << "X" << i << "=" << j * i << '\t';
        }
        cout << '\n';
    }
}

4. 음료 주문

int main() {

	int a;
	char drink[100];
	int total = 0;

	while (true) {
		if (total < 30000) {
			cout << "에스프레소 2000원, 아메리카노 3000원, 카페라테 4000원 \n무엇을 몇 잔 주문하시겠습니까? :";
			cin >> drink >> a;
			if (strcmp(drink, "에스프레소") == 0) {
				cout << a * 2000 << "원 입니다.\n";
				total += (a * 2000);
			}
			else if (strcmp(drink, "아메리카노") == 0) {
				cout << a * 3000 << "원 입니다.\n";
				total += (a * 3000);
			}
			else if (strcmp(drink, "카페라테") == 0) {
				cout << a * 4000 << "원 입니다.\n";
				total += (a * 4000);
			}
			else {
				cout << "잘못 주문하셨어요.\n";
			}
		}
		else {
			cout << "오늘" << total << "원을 판매하여 카페를 닫습니다. 내일봐요";
			break;
		}
	}
}
#include <iostream>
#include <cstdlib>
using namespace std;

int main() {

    char drink[100];
    char number[1000];    
    int total = 0;

    while (true) {
        if (total < 30000) {
            cout << "에스프레소 2000원, 아메리카노 3000원, 카페라테 4000원 \n무엇을 몇 잔 주문하시겠습니까? :";
            cin.ignore();
            cin >> drink >> number;
            
            int a = atoi(number);

            if (a == 0) {
                cout << "입력오류.\n";
                continue;
            }

            if (strcmp(drink, "에스프레소") == 0) {
                cout << a * 2000 << "원 입니다. 맛있게 드세요\n";
                total += (a * 2000);
            } else if (strcmp(drink, "아메리카노") == 0) {
                cout << a * 3000 << "원 입니다. 맛있게 드세요\n";
                total += (a * 3000);
            } else if (strcmp(drink, "카페라테") == 0) {
                cout << a * 4000 << "원 입니다. 맛있게 드세요\n";
                total += (a * 4000);
            } else {
                cout << "잘못 주문하셨어요.\n";
            }            
        } else {
            cout << "오늘" << total << "원을 판매하여 카페를 닫습니다. 내일봐요";
            break;
        }
    }

'C++' 카테고리의 다른 글

노래 인기도 데이터 군집 분석_메뉴얼  (0) 2024.02.20
C++ namespace ~~~ 객체  (0) 2023.05.19
C++  (0) 2023.05.18
C++  (0) 2023.05.18