- 실패 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
String[] input = reader.readLine().split(" ");
int A = Integer.parseInt(input[0]);
int B = Integer.parseInt(input[1]);
int C = Integer.parseInt(input[2]);
System.out.println(A + B + C);
}
}
위 코드를 그대로 실행하면 런타임에러(NumberFormat)가 난다.
그 이유는 정수형 int의 범위는 **-2,147,483,648**부터 **2,147,483,647**까지이다.
즉, 2^31 -1까지의 범위, 21억까지인데 문제에는 10^12 즉, 1조가 된다.
따라서, int형보다 더 큰 long으로 해줘야 문제를 풀 수 있다.
- 성공 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
String[] input = reader.readLine().split(" ");
long A = Long.parseLong(input[0]);
long B = Long.parseLong(input[1]);
long C = Long.parseLong(input[2]);
System.out.println(A + B + C);
}
}