791. 高精度加法
作者:
dsf2
,
2021-03-01 04:46:08
,
所有人可见
,
阅读 383
import java.util.*;
class Main {
public static void main(String[] args) {
Deque<Integer> sum = new LinkedList<>();
Scanner sc = new Scanner(System.in);
String n = sc.nextLine(), m = sc.nextLine();
Deque<Integer> nList = new LinkedList<>();
Deque<Integer> mList = new LinkedList<>();
for (int i = 0; i < n.length(); i++)
nList.offerFirst(n.charAt(i) - '0');
for (int i = 0; i < m.length(); i++)
mList.offerFirst(m.charAt(i) - '0');
int i = 0, j = 0;
int s = 0;
while (nList.size() > 0 || mList.size() > 0) {
int currN = nList.size() > 0 ? nList.pollFirst() : 0;
int currM = mList.size() > 0 ? mList.pollFirst() : 0;
s += currN + currM;
sum.offerLast(s % 10);
s /= 10;
}
if (s > 0)
sum.add(s);
while (sum.size() > 0)
System.out.printf("%d", sum.pollLast());
}
}