[BaekJoon] 11718번 - 그대로 출력하기 [Java][C++]
[BaekJoon] 11718번 - 그대로 출력하기 [Java][C++]
1. 문제 풀이
주어진 입력 그대로 출력하는 문제로 입력이 얼마나 주어지는지는 알 수 없다. EOF를 활용해서 읽을 수 없을 때까지 읽으며 출력하면 된다.
2. 코드
1. 풀이 [Java]
BufferedReader의 readLine 메서드는 EOF에서 null을 반환함을 활용해서 입력을 받고 BufferedWriter로 출력했다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
bw.flush();
}
}
2. 풀이 [C++]
getline 함수로 줄 단위로 입력을 받아서 그대로 출력했다. getline은 EOF에서 false로 평가됨을 활용해 무한 루프의 조건식으로 활용했다.
1
2
3
4
5
6
7
8
9
10
11
12
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string line;
while (getline(cin, line)) {
cout << line << '\n';
}
}
This post is licensed under CC BY 4.0 by the author.