Hdu

题目:

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 167825    Accepted Submission(s): 32125


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
  太久没有写高精度,今天写了一下高精度加法,第一次写高精度的时候用了80+行,后来改到<40行,结果现在太久没有写,一写就是70+ = =,当然这次和以前写的有点不一样,以前写的就是纯粹地为了得到那个答案,这次写有一点像是做模板的性质(虽然感觉这模板不可恭维= =),这次写的是写在一个结构体里面,这样是为了在需要高精度的运算的时候可以更加方便的使用(只是自己感觉方便使用= =),而且我把一个大数定义成结构体,结构体里面除了大数加法的函数以外还有大数和大数的长度以及大数由低位到高位的写法,方便以后添加高精度剪发,乘法,除法。
 
上代码:
 
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define MAX 1050
 4 using namespace std;
 5 
 6 typedef struct N
 7 {
 8     char num[MAX],anum[MAX];
 9     int len;
10     void anti()
11     {
12         int i;
13         for(i=0;i<len;i++)
14         {
15             anum[i]=num[len-i-1];
16         }
17     }
18     void big_plus(N a,N b)
19     {
20         int c,i;
21         c=0;
22         memset(num,0,sizeof(num));
23         memset(anum,0,sizeof(anum));
24         len=0;
25         for(len=0;len<a.len && len<b.len;len++)
26         {
27             int temp=((a.anum[len]-'0')+(b.anum[len]-'0')+c);
28             c=temp/10;;
29             anum[len]=temp%10+'0';
30         }
31         while(len<a.len)
32         {
33             int temp=(a.anum[len]-'0')+c;
34             c=temp/10;
35             anum[len++]=temp%10+'0';
36         }
37         while(len<b.len)
38         {
39             int temp=(b.anum[len]-'0')+c;
40             c=temp/10;
41             anum[len++]=temp%10+'0';
42         }
43         if(c) anum[len++]='1';
44         while(len && anum[len-1]=='0') len--;
45         if(!len){anum[len++]='0';}
46         for(i=0;i<len;i++)
47         {
48             num[i]=anum[len-i-1];
49         }
50     }
51 }N;
52 
53 
54 int main()
55 {
56     int i,t;
57     N n1,n2,n3;
58     //freopen("data.txt","r",stdin);
59     scanf("%d",&t);
60     for(i=1;i<=t;i++)
61     {
62         scanf("%s %s",n1.num,n2.num);
63         n1.len=strlen(n1.num);
64         n2.len=strlen(n2.num);
65         n1.anti();
66         n2.anti();
67         n3.big_plus(n1,n2);
68         printf("Case %d:
",i);
69         printf("%s + %s = %s
",n1.num,n2.num,n3.num);
70         if(i!=t) printf("
");
71     }
72     return 0;
73 }
1002
 
原文地址:https://www.cnblogs.com/sineatos/p/3265585.html