首页 > 代码库 > hdu_1002_大数
hdu_1002_大数
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 201400 Accepted Submission(s): 38599
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
大数问题,总觉得自己写的麻烦了,不解释。。。Java 和 C ~~~~
Java 代码:
1 import java.math.BigDecimal; 2 import java.util.*; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n = cin.nextInt(); 8 for (int i = 1; i <= n; ++i) { 9 10 BigDecimal a1, b1, c1; 11 a1 = cin.nextBigDecimal(); 12 b1 = cin.nextBigDecimal(); 13 c1 = a1.add(b1); 14 System.out.println("Case " + i + ":"); 15 System.out.println(a1 + " + " + b1 + " = " + c1); 16 if (i < n) 17 System.out.println(); 18 } 19 } 20 }
C代码:
1 #include <cstdio> 2 #include <iostream> 3 #include <string.h> 4 using namespace std; 5 6 #define N 1000 7 int main() 8 { 9 int T, f; 10 int m, n, temp; 11 char A[N], B[N]; 12 int sum[N+1], t[N]; 13 14 cin>>T; 15 f=T; 16 17 while(T--) 18 { 19 scanf("%s",A); 20 scanf("%s",B); 21 22 m = strlen(A); 23 n = strlen(B); 24 25 if(m==n) { 26 27 for(int i = m-1; i>=0; i--) t[i]=A[i]+B[i]-‘0‘-‘0‘; 28 29 for(int i = m-1; i>=0; i--) { 30 31 temp = t[i]/10; 32 sum[i+1] = t[i]%10; 33 if(temp && i) t[i-1] += temp; 34 if(i==0) sum[0] = temp; 35 } 36 37 printf("Case %d:\n",f-T); 38 printf("%s + %s = ",A,B); 39 40 if(sum[0]) printf("%d", sum[0]); 41 42 for(int i = 1; i < m; i++) printf("%d", sum[i]); 43 44 printf("%d\n",sum[m]); 45 } 46 else if(m>n) { 47 48 for(int i = m-1; i>=0; i--) t[i]=A[i]-‘0‘; 49 for(int i = m-1; n>=m-i; i--) t[i]+=B[n-m+i]-‘0‘; 50 51 for(int i = m-1; i>=0; i--) { 52 53 temp = t[i]/10; 54 sum[i+1] = t[i]%10; 55 if(temp && i) t[i-1] += temp; 56 if(i==0) sum[0] = temp; 57 } 58 59 printf("Case %d:\n",f-T); 60 printf("%s + %s = ",A,B); 61 62 if(sum[0]) printf("%d", sum[0]); 63 64 for(int i = 1; i < m; i++) printf("%d", sum[i]); 65 66 printf("%d\n",sum[m]); 67 68 } 69 else { 70 71 for(int i = n-1; i>=0; i--) t[i]=B[i]-‘0‘; 72 for(int i = n-1; m>=n-i; i--) t[i]+=A[m-n+i]-‘0‘; 73 74 for(int i = n-1; i>=0; i--) { 75 76 temp = t[i]/10; 77 sum[i+1] = t[i]%10; 78 if(temp && i) t[i-1] += temp; 79 if(i==0) sum[0] = temp; 80 } 81 82 printf("Case %d:\n",f-T); 83 printf("%s + %s = ",A,B); 84 85 if(sum[0]) printf("%d", sum[0]); 86 87 for(int i = 1; i < n; i++) printf("%d", sum[i]); 88 89 printf("%d\n",sum[n]); 90 91 } 92 if(T) printf("\n"); 93 } 94 return 0; 95 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。