Problem A+B(Big Integer)

复制代码
/*========================================================================
Problem A+B(Big Integer)
Time Limit:1000MS Memory Limit:65536KB
Total Submit:3205 Accepted:922
Description 
Give two positive integer A and B,calucate A+B.
Notice that A,B is no more than 500 digits.
Input 
The test case contain several lines.Each line contains two positive integer A and B.
Output 
For each input line,output a line contain A+B
Sample Input 
2 3
1231231231823192 123123123123123
1000000000000000 1
Sample Output 
5
1354354354946315
1000000000000001
Source
EOJ
==========================================================================*/
复制代码

http://202.120.80.191/problem.php?problemid=1001

复制代码
 1 #include<stdio.h>
 2 #include<string.h>
 3 void sum(char a[],char b[],char c[]);//c=a+b
 4 void swap(char a[]);
 5 int main()
 6 {
 7     char a[503],b[503],c[505];
 8     freopen("5.in","r",stdin);
 9     while(scanf("%s%s",a,b)!=EOF)//while(cin>>a>>b)
10     {
11         sum(a,b,c);
12         printf("%s\n",c);
13     }
14     return 0;
15 }
16 void sum(char a[],char b[],char c[])//c=a+b
17 {
18     int i,lenA,lenB,min,max;
19     int carry=0,t;
20     lenA=strlen(a);
21     lenB=strlen(b);
22     swap(a);
23     swap(b);
24     if(lenA>lenB)
25     {
26         max=lenA;
27         min=lenB;
28     }
29     else
30     {
31         max=lenB;
32         min=lenA;
33     }
34     for(i=0;i<min;i++)
35     {
36         t=(a[i]-'0')+(b[i]-'0')+carry;
37         c[i]=t%10+'0';
38         carry=t/10;
39     }
40     if(lenA>lenB)
41     {
42         for(i=min;i<max;i++)
43         {
44             t=(a[i]-'0')+carry;
45             c[i]=t%10+'0';
46             carry=t/10;
47         }
48     }
49     else
50     {
51         for(i=min;i<max;i++)
52         {
53             t=(b[i]-'0')+carry;
54             c[i]=t%10+'0';
55             carry=t/10;
56         }
57     }
58     if(carry!=0)
59     {
60         c[i]=carry+'0';
61         i++;
62     }
63     c[i]='\0';
64     swap(c);
65 }
66 void swap(char a[])
67 {
68     int i,len=strlen(a),t=len/2;
69     char ch;
70     for(i=0;i<t;i++)
71     {
72         ch=a[i];
73         a[i]=a[len-1-i];
74         a[len-1-i]=ch;
75     }
76 }
View Code
复制代码

 

 

 

 

posted on   华山青竹  阅读(700)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示