AcWing 791. 高精度加法-Java数组
原题链接
简单
作者:
装酷的小胖子
,
2021-04-01 15:37:03
,
所有人可见
,
阅读 261
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import static java.lang.System.*;
public class Main{
public static void main(String[] agrs) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String astr = br.readLine();
String bstr = br.readLine();
int[] a = new int[astr.length()];
int[] b = new int[bstr.length()];
for(int i = a.length - 1, j = 0; i >= 0; i--, j++) a[j] = astr.charAt(i) - '0';
for(int i = b.length - 1, j = 0; i >= 0; i--, j++) b[j] = bstr.charAt(i) - '0';
int len = a.length > b.length ? a.length : b.length;
int[] c = new int[len];
int t = 0;
for(int i = 0; i < len; i++){
if(i < a.length) t += a[i];
if(i < b.length) t += b[i];
c[i] = t % 10;
t = t / 10;
}
if(t > 0){
out.print(1);
}
for(int i = len -1; i >= 0; i--){
out.print(c[i]);
}
}
}