欢迎使用CSDN-markdown编辑器

来源:互联网 发布:武藏点胶机编程 编辑:程序博客网 时间:2024/05/21 19:39

A + B Problem II

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

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

Author
Ignatius.L

可以说是很耿直的做法了······

#include<stdio.h>#include<iostream>#include<string.h>using namespace std;string s1,s2;int sum[1005];int t,l1,l2;int main(){    cin>>t;    for(int i=1;i<=t;i++){        cin>>s1>>s2;        l1=s1.size();        l2=s2.size();        int p=0;        int l=0;        for(;l1>0||l2>0;l1--,l2--){            if (l1>0&&l2>0){           //s1和s2都有第l位                sum[l]=(((s1[l1-1]-'0')+(s2[l2-1])-'0')+p)%10;                p=((s1[l1-1]-'0')+(s2[l2-1]-'0')+p)/10;            }else if(l1<=0){          //s1有第l位                sum[l]=(((s2[l2-1])-'0')+p)%10;                p=((s2[l2-1]-'0')+p)/10;            }else{                    //s2有第l位                sum[l]=((s1[l1-1]-'0')+p)%10;                p=((s1[l1-1]-'0')+p)/10;            }            l++;        }        cout<<"Case "<<i<<":"<<endl<<s1<<" + "<<s2<<" = ";        if(p>0) cout<<'1';    //注意最后第一位的进位,否则和我一样一直wa就不好玩了        for(int m=l-1;m>=0;m--)            cout<<sum[m];        cout<<endl;        if (i!=t) cout<<endl;        }        return 0;            }
原创粉丝点击