728x90
문제
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 A와 B가 주어진다. (0 < A,B < 1010000)
출력
첫째 줄에 A+B를 출력한다.
예제 입력 1
9223372036854775807 9223372036854775808
예제 출력 1
18446744073709551615
// 코드
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
32
33
34
35
36
37
38
39
40
41
42
43
|
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws Exception {
// Please Enter Your Code Here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
String str_A = st.nextToken();
String str_B = st.nextToken();
int maxLength = Math.max(str_A.length(), str_B.length());
int[] A = new int[maxLength + 1];
int[] B = new int[maxLength + 1];
// A 초기화
for(int i=str_A.length()-1, idx=0; i>=0; i--, idx++) {
A[idx] = Integer.parseInt(str_A.charAt(i)+"");
}
// B 초기화
for(int i=str_B.length()-1, idx=0; i>=0; i--, idx++) {
B[idx] = Integer.parseInt(str_B.charAt(i)+"");
}
for(int i=0; i<maxLength; i++) {
int value = A[i] + B[i];
A[i] = value%10;
A[i+1] += (value/10);
}
if(A[maxLength] == 0) {
for(int i=maxLength-1; i>=0; i--) {
System.out.print(A[i] + "");
}
} else {
for(int i=maxLength; i>=0; i--) {
System.out.print(A[i] + "");
}
}
}
}
|
cs |
반응형