문제 출처: www.acmicpc.net/problem/10869
10869번: 사칙연산
두 자연수 A와 B가 주어진다. 이때, A+B, A-B, A*B, A/B(몫), A%B(나머지)를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
1. 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main(void) | |
{ | |
int A, B; | |
cin >> A >> B; | |
cout << A + B << endl << A - B << endl << A * B << endl << A / B << endl << A % B; | |
return 0; | |
} |
2. 풀이
cout를 사용할 때 endl 혹은 \n을 사용하면 줄 바꿈을 할 수 있다. 그래서 그것을 이용해서 한 줄로 출력했다. 추가적으로 A / B를 할 때는 A를 B로 나누었을 때 몫이 나오게 되고 A % B를 하게 되면 A를 B로 나누었을 때 나머지가 나오게 된다.
'컴퓨터 > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 2588번: 곱셈 [C++] (0) | 2020.11.22 |
---|---|
백준 알고리즘 10430번: 나머지 [C++] (0) | 2020.11.21 |
백준 알고리즘 1008번: A/B [C++] (0) | 2020.11.19 |
백준 알고리즘 1914번: 하노이 탑 [C++] (0) | 2020.11.18 |
백준 알고리즘 10172번: 개 [C++] (0) | 2020.11.14 |