题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String first = br.readLine(); String second = br.readLine(); int[] res = sum(first,second); StringBuilder sb = new StringBuilder(); for(int i=res.length-1;i>=0;i--){ if(i==res.length-1&&res[i]==0){ continue; } sb.append(res[i]); } System.out.println(sb); } public static int[] sum(String first,String second){ if(first.length()<second.length()){ return sum(second,first); } int m = first.length(); int n = second.length(); for(int i=0;i<m-n;i++){ second = "0"+second; } int[] res = new int[m+1]; int index=0; int sum = 0; for(int i=m-1;i>=0;i--){ sum = res[index]+first.charAt(i)+second.charAt(i)-'0'-'0'; if(sum>=10){ res[index] = sum%10; res[index+1] = 1; } else { res[index] = sum%10; } index++; sum=0; } return res; } }