A+B problem

yjjlyyj151 2011-01-20 08:10:38
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

====================================================================


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
int main()
{

int c,k=0,f,a,sum,s; //f表示进位否,C表示一共有几次计算,sum表示,当前V1的数与当前V2的数相加的和
char ss1[80],ss2[80],cc;//Cc表示从S1,S2中提取的单个字符
cin>>c;
while(1)
{
f=0;
s=0;
vector<int> v1,v2,v3;
string s1,s2,s3;
scanf("%s",ss1);
scanf("%s",ss2);
s1=ss1;
s2=ss2;
string::iterator sit1,sit2;
for(sit1=s1.begin();sit1!=s1.end();sit1++)
{
cc=(*sit1);
sscanf(&cc,"%d",&a);//把字符转成数字
if(a==0 &&s==0) //表示前面0如0000009
continue;
else
{
v1.push_back(a);
s=1; //表示前面没有0了
}
}
s=0;
for(sit2=s2.begin();sit2!=s2.end();sit2++)
{
cc=(*sit2);
sscanf(&cc,"%d",&a);
if(s==0 && a==0)
continue;
else
{
v2.push_back(a);
s=1;
}
}
std::reverse(v1.begin(),v1.end());//翻转
std::reverse (v2.begin(),v2.end());
vector<int>::iterator vit,vit2;
for(vit=v1.begin(),vit2=v2.begin();;)
{
sum=0;
if(vit==v1.end()&&vit2!=v2.end())//如果V1向量到底了,V2却没有
{
if(f==0)
v3.push_back(*vit2);//如果没有进位
else if(f==1)
{
if(*vit2+1<10)
{
v3.push_back(*vit2+1); // 如果进位
f=0;
}
else //如果大于10 F=1
{
v3.push_back(*vit2-10+1);
f=1;
}
}
vit2++;
if(vit2==v2.end()&&f==1) //如果连V2也到底了,那就结束FOR
{
v3.push_back(1);
f=0;
}
if(vit2==v2.end()&&f==0)
break;
else
continue;//否则就再循环

}
else if(vit2==v2.end() && vit!=v1.end())//与上面相反
{
if(f==0)
v3.push_back(*vit);
else
{
if(*vit+1<10)
{
v3.push_back(*vit+1);
f=0;
}
else
{
v3.push_back(*vit-10+1);
f=1;
}
}
vit++;
if(vit==v1.end()&&f==1)
{
v3.push_back(1);
f=0;
}
if(vit==v1.end()&&f==0)
break;
else
continue;
}
else if(vit==v1.end() && vit2==v2.end())
break;

if(f==0)//如果V1,V2相对应的有数,那么就相加
{
if(*vit+*vit2>=10)
{
sum=*vit+*vit2;
f=1;
v3.push_back(sum-10);
}
else
{
sum=*vit+*vit2;
f=0;
v3.push_back(sum);
}
}
else if(f==1)
{
if(*vit+*vit2+1>=10)
{
sum=*vit+*vit2;
f=1;
v3.push_back(sum-10+1);
}
else
{
sum=*vit+*vit2;
f=0;
v3.push_back(sum+1);
}
}
if(vit==v1.end()-1 && vit2==v2.end()-1 && f==1)//如果都到底了,但是F有进位,V3再赋一个1
v3.push_back(1);
vit++;
vit2++;
}
std::reverse(v1.begin(),v1.end());//因为刚才为了相加而翻转了,现在为了输同,要翻转过来
std::reverse (v2.begin(),v2.end());
std::reverse(v3.begin(),v3.end());//因为V1,V2是倒着加的,V3是顺着赋值的,所以要得到正确答案,V3要翻转
vector<int>::iterator vit3;
cout<<"Case "<<k+1<<":"<<endl;
for(vit=v1.begin();vit!=v1.end();vit++)
cout<<*vit;
cout<<" + ";
for(vit2=v2.begin();vit2!=v2.end();vit2++)
cout<<*vit2;
cout<<" = ";
for(vit3=v3.begin();vit3!=v3.end();vit3++)
cout<<*vit3;
k++;
if(k==c)
break;
else
cout<<endl<<endl;
}
}



可能代码太长了,一直WA。请高手指点。难道不能以字符输入吗
...全文
600 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
logiciel 2011-01-20
  • 打赏
  • 举报
回复
设输入是:
0 1

应输出
0 + 1 = 1

但LZ程序输出
+ 1 = 1
zy020118 2011-01-20
  • 打赏
  • 举报
回复
是杭电ACM的1002题吧,话说楼主是不是最后少了个回车
前几天刚帮另一个看了一下,我提交通过的程序在这里http://topic.csdn.net/u/20110118/15/d2e67e41-6b7b-48de-a87a-95846678777a.html
ayw215 2011-01-20
  • 打赏
  • 举报
回复
我晕,不就是个大数加法么,你怎么搞这么麻烦
qq120848369 2011-01-20
  • 打赏
  • 举报
回复
不用这么复杂的....

你定义三个足够大的字符数组A,B,C,都memset为0,然后读入A,读入B,数组C是进位数组.
由于读入的数字最高位在数组的最低位,所以对A,B分别作reverse.

现在,就可以继续A+B了,都是从数组下标0开始算就行了,每一位的运算都是A[i]+B[i]+C[i].

假设每个数组的长度都是1000,那就for循环直到1000就行了,即便最高位已经变成了0+0+0,这样写代码比较

短.

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧