문제 출처: www.acmicpc.net/problem/10951
10951번: A+B - 4
두 정수 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) | |
{ | |
while (1) | |
{ | |
int a, b; | |
cin >> a >> b; | |
if (cin.eof() == true) | |
break; | |
cout << a + b << endl; | |
} | |
return 0; | |
} |
2. 풀이
while (1)
우선 문제를 읽어보면 알겠지만 종료시점이 나오지 않는다. 그렇다는 것은 EOF(end of file)이 적용됬다는 생각을 가지고 무한반복문을 생성한다.
int a, b;
cin >> a >> b;
그리고 a와 b를 선언 후 입력을 통한 초기화를 진행한다
if (cin.eof() == true)
break;
그리고 입력을 했을 때 cin에 eof가 입력되었는지 검사를 한 뒤에 만약 eof가 입력이 됬으면 반복문을 탈출한다.
cout << a + b << endl;
그리고 a + b의 값을 출력한다
'컴퓨터 > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 2562번: 최댓값 (0) | 2020.12.26 |
---|---|
백준 알고리즘 10818번: 최소, 최대 [C++] (0) | 2020.12.21 |
백준 알고리즘 10952번: A+B - 5 [C++] (0) | 2020.12.18 |
백준 알고리즘 10871번: X보다 작은 수 [C++] (0) | 2020.12.16 |
백준 알고리즘 2439번: 별 찍기 - 2 [C++] (0) | 2020.12.15 |