2 1 2 112233445566778899 998877665544332211
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
public static void bigNumAdd(){ Scanner s = new Scanner(System.in); int a = s.nextInt(); int b = 0; int c[] = null; int d[] = null; int sum[] = null; while(++b <= a){ c = new int[1000]; d = new int[1000]; sum = new int[1001]; String ss = s.next(); String bb = s.next(); int chlen = ss.length(); int shlen = bb.length(); char ch[] = ss.toCharArray(); char sh[] = bb.toCharArray(); System.out.println("Case "+b+":"); for(int i=0;i<chlen;i++){ c[i] = Integer.parseInt(ch[i]+""); System.out.print(c[i]); } System.out.print(" + "); for(int j=0;j<shlen;j++){ d[j] = Integer.parseInt(sh[j]+""); System.out.print(d[j]); } System.out.print(" = "); int sumlen = 1001; while(chlen-1 >=0 || shlen -1 >=0){//将两个数组的值相加,结果放到第三个数组中 if(chlen <= 0 && shlen > 0){ sum[--sumlen] = d[--shlen]; } if(chlen > 0 && shlen <= 0){ sum[--sumlen] = c[--chlen]; } if(chlen > 0 && shlen > 0) { sum[--sumlen] = c[--chlen] + d[--shlen]; } } for (int k = 1000; k > 0; k--) {//对第三个数组的结果进行处理,特别是超出10的部分。 if ( sum[k] > 9) { sum[k] = sum[k] - 10; sum[k - 1]++; } } int flag = 0; for(int q=0;q<sum.length;q++){ if(sum[q] == 0 && flag == 0){//对于默认填充0的数组元素,不输出。 continue; } flag =1; System.out.print(sum[q]); } System.out.println();//万恶的系统格式处理 if(a !=b){ System.out.println(); } }//while }
运行结果: 2 12345 11 Case 1: 12345 + 11 = 12356 11 1 Case 2: 11 + 1 = 12
版权声明:本文为博主原创文章,转载请注明出处:http://blog.csdn.net/lingzhm
原文地址:http://blog.csdn.net/lingzhm/article/details/47362591