杭电OJ-1001-(Sum Problem大整数求和问题)
2015-08-03 16:20
239 查看
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 in the equation. Output a blank line between two test cases.
Sample Input:
4
1 2
5 5
112233445566778899 998877665544332211
112233445566778893355877665544332211998877665544332211112233445566778823 998877665544332211112233445566778823112233445566778893355877665544332211
Sample Output:
Case 1:
1 + 2 = 3
Case 2:
5 + 5 = 10
Case 3:
112233445566778899 + 998877665544332211 = 1111111111111111110
Case 4:
112233445566778893355877665544332211998877665544332211112233445566778823 + 998877665544332211112233445566778823112233445566778893355877665544332211 = 1111111111111111104468111111111111035111111111111111104468111111111111034
解法1:
利用Java语言的良好特性:
解法2:
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 in the equation. Output a blank line between two test cases.
Sample Input:
4
1 2
5 5
112233445566778899 998877665544332211
112233445566778893355877665544332211998877665544332211112233445566778823 998877665544332211112233445566778823112233445566778893355877665544332211
Sample Output:
Case 1:
1 + 2 = 3
Case 2:
5 + 5 = 10
Case 3:
112233445566778899 + 998877665544332211 = 1111111111111111110
Case 4:
112233445566778893355877665544332211998877665544332211112233445566778823 + 998877665544332211112233445566778823112233445566778893355877665544332211 = 1111111111111111104468111111111111035111111111111111104468111111111111034
解法1:
利用Java语言的良好特性:
[code]import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int len =0; while(input.hasNext()){ len = input.nextInt(); for(int i=1 ; i<= len ;i++){ BigInteger a = new BigInteger(input.next()); BigInteger b = new BigInteger(input.next()); System.out.println("Case "+i+":"); System.out.println(a+" + "+b+" = "+a.add(b)); if(i<=len-1){ System.out.println(); } } } } }
解法2:
[code]import java.util.Scanner; public class Main{ static int N=1000; public static void main(String[] args) { int a[]; int b[]; Scanner input=new Scanner(System.in); while(input.hasNext()){ int len = input.nextInt(); for(int i=1 ; i<= len ; i++){ a = getDigit(input.next()); b = getDigit(input.next()); System.out.println("Case "+i+":"); System.out.println(getString(a)+" + "+ getString(b)+" = "+ getString(add(a,b))); if(i<=len-1){ System.out.println(); } } } } public static String getString(int array []){ String str ="" ; int flag= array.length-1; while(array[flag]==0) { flag--; if(flag==-1) { return "0" ; } } for(int i=flag;i>=0;i--) { str += array[i]; } return str; } private static int[] add(int a[], int b[]) { int [] result = new int [N+1]; for(int i=0; i<N ;i++) { result[i]=a[i]+b[i]; //先将每个数组对应位上做加法 } for(int i=0; i<N ;i++) { result[i+1] += result[i]/10; //加法进位 result[i] = result[i]%10; //余数 } return result; } private static int[] getDigit(String s) { int array [] = new int ; int len= s.length(); for(int i=0;i<len;i++) { array[i]=s.charAt(len-1-i)-'0'; } return array; } }
相关文章推荐
- Java byte数组 转short int
- SVN 权限配置详细说明
- Activity生命周期(二)——多个Activity交互
- php header解决乱码问题及其他乱码问题
- oracle 日期获取
- 更新yum源/apt-get源
- 避免野指针的产生
- poj 2299
- Linux命令之ls
- hdoj2070斐波那契数列
- mysql的死锁等6个实战问题解决
- 浅谈iOS中MVVM的架构设计与团队协作
- 树
- Ubuntu14.04更新源
- HDU 2553 N皇后问题
- poj 3126 Prime Path 【bfs】
- LMS算法
- linux中挂载硬盘报错(you must specify the filesystem type)
- 第二次完成与第一次一模一样的任务
- LeetCode - Shortest Palindrome (KMP)