蓝桥杯——高精度加法
程序员文章站
2024-03-17 11:19:58
...
package first_package;
import java.util.*;
import java.lang.Math;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inScanner = new Scanner(System.in);
String a = inScanner.next();
String b = inScanner.next();
String c = new String();
int temporary = 0;
int sum=0;
int indexa = 0;
int indexb =0;
for(int i =1; i<=Math.max(a.length(),b.length());i++){
indexa = a.length()-i;
indexb = b.length()-i;
if(i <= Math.min(a.length(),b.length())){
sum = Character.getNumericValue(a.charAt(indexa))+ Character.getNumericValue(b.charAt(indexb))+temporary;
c =String.valueOf(sum%10)+ c;
temporary = sum/10;
}
else if(indexa<0){
sum = Character.getNumericValue(b.charAt(indexb)) + temporary;
c =String.valueOf(sum%10)+ c;
temporary = sum/10;
}
else{
sum = Character.getNumericValue(a.charAt(indexa)) + temporary;
c =String.valueOf(sum%10)+ c;
temporary = sum/10;
}
}
if (temporary >0){
c =String.valueOf(temporary%10)+ c;
}
System.out.print(c);
}
}
上一篇: 前端和java后台的数据交互