输入包括两个数a和b,其中a和b的位数不超过1000位。
可能有多组测试数据,对于每组数据, 输出a+b的值。
2 6 10000000000000000000 10000000000000000000000000000000
8 10000000000010000000000000000000
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s; while ((s = br.readLine()) != null) { String[] str = s.split(" "); BigInteger a = new BigInteger(str[0]); BigInteger b = new BigInteger(str[1]); System.out.println(a.add(b)); } } }
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) System.out.println(new BigInteger(scanner.next()).add(new BigInteger(scanner.next()))); } }
import java.util.*; import java.math.*; public class Main{ public static void main(String args[]) { Scanner cin = new Scanner(System.in); BigInteger a,b; BigInteger sum; while(cin.hasNextBigInteger()) { a=cin.nextBigInteger(); b=cin.nextBigInteger(); sum=a.add(b); System.out.println(sum); } } }应该要用恰当的方法解决给定问题不是么??所以->BigInteger,没毛病。
import java.util.Scanner; import java.math.BigInteger; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ BigInteger bi1 = new BigInteger(sc.next()); BigInteger bi2 = new BigInteger(sc.next()); System.out.println(bi1.add(bi2)); } } }
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//java A+B
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = input.readLine()) != null) {
String num[] = line.split(" ");
System.out.println(sum(num[0],num[1]));
}
input.close();
}
static String sum(String n,String m){
String min=n.length()>m.length()?m:n;
String max=n.length()>m.length()?n:m;
int maxLen=max.length();
int minLen=min.length();
int i;//max的定位
int len=min.length();
int count=0;
StringBuffer sb=new StringBuffer();
for(i=0;i<len;i++){
int num=min.charAt(minLen-i-1)+max.charAt(maxLen-i-1)-'0'-'0'+count;
count=num/10;
sb.append(num%10);
}
while(count!=0&&maxLen>i){
int num=max.charAt(maxLen-i-1)+count-'0';
count=num/10;
sb.append(num%10);
i++;
}
if(count!=0){
sb.append(count);
}
return max.substring(0, maxLen-i)+sb.reverse().toString();
}
}
import java.math.BigInteger; import java.util.Scanner; /** * Created by fhqplzj on 17-2-3 at 下午1:10. */ public class My1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { BigInteger a = new BigInteger(scanner.next()); BigInteger b = new BigInteger(scanner.next()); System.out.println(a.add(b)); } } }