HDOJ 1002
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 124589 Accepted Submission(s): 23993
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
1 #include <iostream> 2 using namespace std; 3 4 void sum(char (&a)[1001],char (&b)[1001]); 5 int main() 6 { 7 int T, count_T; 8 char a[1001], b[1001]; 9 cin >> T; 10 for(count_T=1; count_T <= T; count_T++) 11 { 12 cin >> a >> b; 13 14 if(count_T==1) 15 { 16 cout << "Case " << count_T << ":" << endl; 17 } 18 else 19 { 20 cout << "\nCase " << count_T << ":" << endl; 21 } 22 23 cout << a << " + " << b << " = "; 24 25 sum(a,b); 26 } 27 return(0); 28 } 29 void sum(char (&a)[1001],char (&b)[1001]) 30 { 31 char c[1002]; 32 int la, lb; 33 la=strlen(a)-1; 34 lb=strlen(b)-1; 35 int i; 36 int flag=0; 37 for(i=0;la>=0 && lb>=0;la--,lb--,i++) 38 { 39 flag+=(a[la]-'0')+(b[lb]-'0'); 40 c[i]=flag%10; 41 flag/=10; 42 } 43 for(la=la;la>=0;la--,i++) 44 { 45 flag+=(a[la]-'0'); 46 c[i]=flag%10; 47 flag/=10; 48 } 49 for(lb=lb;lb>=0;lb--,i++) 50 { 51 flag+=(b[lb]-'0'); 52 c[i]=flag%10; 53 flag/=10; 54 } 55 if(flag) 56 { 57 c[i]=flag; 58 i++; 59 } 60 for(i--;i>=0;i--) 61 { 62 cout<<(char)(c[i]+'0'); 63 } 64 cout<<endl; 65 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?