杭电OJ 1002 大数相加
发布时间:2020-12-14 02:25:40 所属栏目:大数据 来源:网络整理
导读: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 li
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
?
Sample Output
?
输出格式是:结果之间空一行,最后的结果后面无空行,但是光标一定得移动到结果的下一行,否则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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |