poj1000
A+B
//Memory Time //2996K 750MS import java.io.*; import java.util.*; public class Main { public static void main(String args[]) throws Exception { Scanner cin=new Scanner(System.in); int a=cin.nextInt(),b=cin.nextInt(); System.out.println(a+b); } }
在输入输出为int型范围(-2147483648 到2147483647) 计算正常,否则
Exception in thread "main" java.util.InputMismatchException: For input string: "1234567984564313221"
at java.util.Scanner.nextInt(Scanner.java:2123)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:10)
实现超大数字的A+B代码如下:
import java.io.*; import java.util.*; import java.lang.String; import java.math.BigDecimal; public class Main { public static void main(String args[]) throws Exception { String s1,s2; Scanner cin=new Scanner(System.in); s1 = cin.next(); s2 = cin.next(); BigDecimal b1 = new BigDecimal(s1); BigDecimal b2 = new BigDecimal(s2); System.out.println(b1.add(b2)); cin.close(); } }
测试:
123456789456789
12345678914561
135802468371350
后端工程师
主写nodejs
业余python,java
nowindxdw@126.com