Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Binary-Search
- number_theory
- sliding-window
- Greedy
- BFS
- bitmask
- backtracking
- knapsack
- stack
- ad_hoc
- lazy-propagation
- Python
- sparse_table
- segment-tree
- DP
- kmp
- java
- C
- math
- Sort
- bruteforcing
- C++
- queue
- PS
- Prefix-Sum
- 백준
- lca
- codeup
- implementation
- string
Archives
- Today
- Total
공작소
[ BOJ ] 10951 : A+B - 4 ( BRONZE 5 ) / C, C++, Python, Java 본문
문제
두 정수 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);
}
}
}
'(예전 글) > PS' 카테고리의 다른 글
[ BOJ ] 3109 : 빵집 ( GOLD 2 ) / C (0) | 2023.09.11 |
---|---|
[ BOJ ] 10807 : 개수 세기 ( BRONZE 5 ) / C, C++, Python, Java (0) | 2023.09.11 |
[ BOJ ] 27114 : 조교의 맹연습 ( GOLD 4 ) / Python (0) | 2023.09.11 |
[ BOJ ] 10952 : A+B - 5 ( BRONZE 5 ) / C, C++, Python, Java (0) | 2023.09.08 |
[ BOJ ] 2439 : 별 찍기 - 2 ( BRONZE 4 ) / C, C++, Python, Java (0) | 2023.09.08 |