import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * 大精度和 * * * Scanner sc=new Scanner(System.in); BigInteger a=sc.nextBigInteger(); BigInteger b=sc.nextBigInteger(); System.out.println(a.add(b)); sc.close(); * 可以直接得出答案 * * *@author wenfan *@version 2018年11月20日上午10:15:59 */ public class BigSum { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String s1=br.readLine(); String s2=br.readLine(); String zero=""; if(s1.length()>s2.length()) { for(int i=0;i<s1.length()-s2.length();i++) { zero+="0"; } s2=zero+s2; }else if(s1.length()<s2.length()) { for(int i=0;i<s2.length()-s1.length();i++) { zero+="0"; } s1=zero+s1; } int num1[]=new int[s1.length()+1]; int num2[]=new int[s2.length()+1]; for(int i=1;i<=s1.length();i++) { num1[i]=Character.getNumericValue(s1.charAt(i-1)); num2[i]=Character.getNumericValue(s2.charAt(i-1)); } int []sum=new int[s1.length()+1]; for(int i=s1.length();i>0;i--) { if((num1[i]+num2[i])>10) { sum[i]=sum[i]+(num1[i]+num2[i])%10; sum[i-1]+=1; }else if((num1[i]+num2[i])<=9&&sum[i]==0){ sum[i]=num1[i]+num2[i]; }else if((num1[i]+num2[i])<9&&sum[i]==1){ sum[i]=num1[i]+num2[i]+sum[i]; } else if((num1[i]+num2[i])==9&&sum[i]==1){ sum[i]=0; sum[i-1]+=1; }else if((num1[i]+num2[i])==10&&sum[i]==0) { sum[i]=0; sum[i-1]+=1; }else if((num1[i]+num2[i])==10&&sum[i]==1) { sum[i]=1; sum[i-1]+=1; } } if(sum[0]==0) { for(int i=1;i<sum.length;i++) { System.out.print(sum[i]); } }else { for(int i=0;i<sum.length;i++) { System.out.print(sum[i]); } } } }
if语句没有优化,见谅。