码迷,mamicode.com
首页 > 其他好文 > 详细

hdu_1002_大数

时间:2014-05-24 04:18:59      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   c   code   

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 代码:
 
bubuko.com,布布扣
 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 }
bubuko.com,布布扣

 

 

  C代码:

 

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

 

hdu_1002_大数,布布扣,bubuko.com

hdu_1002_大数

标签:des   style   class   blog   c   code   

原文地址:http://www.cnblogs.com/sayeter/p/3735732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!