1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner in = new Scanner(System.in); 6 String str1 = in.next(); 7 char[] array1 = str1.toCharArray(); 8 String str2 = in.next(); 9 char[] array2 = str2.toCharArray(); 10 int[] num1 = new int[200]; 11 int[] num2 = new int[200]; 12 for (int i = str1.length() - 1; i >= 0; i--) { 13 num1[str1.length() - 1 - i] = array1[i] - '0'; 14 } 15 for (int i = str2.length() - 1; i >= 0; i--) { 16 num2[str2.length() - 1 - i] = array2[i] - '0'; 17 } 18 int maxn = str1.length() > str2.length() ? str1.length() : str2.length(); 19 int[] sum = new int[200]; 20 int ys = 0; 21 for (int i = 0; i < maxn; i++) { 22 sum[i] = (num1[i] + num2[i] + ys) % 10; 23 ys = (num1[i] + num2[i] + ys) / 10; 24 } 25 if (ys != 0) { 26 System.out.print(1); 27 } 28 for (int j = maxn - 1; j >= 0; j--) { 29 System.out.print(sum[j]); 30 } 31 } 32 }
法二:java
1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 public class Seventeen { 5 public static void main(String[] args) { 6 Scanner in = new Scanner(System.in); 7 BigInteger num1=in.nextBigInteger(); 8 BigInteger num2=in.nextBigInteger(); 9 System.out.println(num1.add(num2)); 10 } 11 }