Problem Description
I have a very simple problem for you. Given two integers A and B,your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow,each line consists of two positive integers,A and B. Notice that the integers are very large,that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case,you should output two lines. The first line is "Case #:",# means the number of the test case. The second line is the an equation "A + B = Sum",Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2 1 2 112233445566778899 998877665544332211
Sample Output
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
输出格式是:结果之间空一行,最后的结果后面无空行,但是光标一定得移动到结果的下一行,否则PE错误。(鄙视这种不明说而纯粹需要靠眼力观察得出的格式要求)
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。