-- 예전 기록/BOJ

[ BOJ ] 10951 : A+B - 4 ( BRONZE 5 ) / C, C++, Python, Java

rejo 2023. 9. 11. 11:38

문제

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.

입력

입력은 여러 개의 테스트 케이스로 이루어져 있다.

각 테스트 케이스는 한 줄로 이루어져 있으며, 각 줄에 A와 B가 주어진다. (0 < A, B < 10)

출력

각 테스트 케이스마다 A+B를 출력한다.

풀이 과정

EOF 를 처리하는 방법에 대해 배우는 문제이다. 기존에는 0이 들어오면 반복문을 종료하면 되는 문제였지만 EOF를 처리하는 것은 생소할 수도 있을 것이다. 선호하는 언어의 EOF를 처리하는 방법을 숙지한다면 이후 EOF 를 다루는 문제를 만날 때 수월하게 해결할 수 있을 것이다.

C

#include <stdio.h>

int main(void) {
    int a, b;
    while(scanf("%d %d", &a, &b) == 2)
        printf("%d\n", a+b);
    return 0;
}

C++

#include <iostream>
using namespace std;

int main(void) {
	while(true) {
		int a, b; cin >> a >> b;
		if (cin.eof()) break;
		cout << a + b << endl;
	}
	return 0;
}

Python

import sys
input = sys.stdin.readline

while True:
    try:
        a, b = map(int, input().rstrip().split())
        print(a+b)
    except:
        break

Java

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int a = sc.nextInt();
            int b = sc.nextInt();

            System.out.println(a+b);
        }
    }
}