📄 hdu1002.cpp
字号:
//*************************************************************************
//*程 序 名:HDu1002.CPP *
//*作 者:FLYLIKEABIRD(LIANG YI MING) *
//*编制时间:2008.09.05 *
//*主要功能: A + B Problem II *
//*************************************************************************
#include<iostream>
using namespace std;
int main()
{
int q,n;
int i,a,b,c,temp,temp1,flag;
char A[1100],B[1100];//two positive integers.the integers are very large.故用字符数组来表示正整数(NB)
int C[1100];//
cin>>n;
if(n<1||n>20)exit(0);
for(q=1;q<=n;q++)
{
if(q!=1)cout<<endl;
cin>>A>>B;
a=strlen(A);//求A的长度
b=strlen(B);//求B的长度
// cout<<"Case "<<q<<':'<<endl;
cout<<"Case"<<" "<<q<<":"<<endl;
cout<<A<<" "<<"+"<<" "<<B<<" "<<"="<<" ";
// cout<<A<<" + "<<B<<" = ";
flag=0;
if(a>b)
{
c=a;
temp1=a-b;
for(i=c-1;i>=temp1;i--)
{
temp=A[i]-'0'+(B[i-temp1]-'0');
if(flag)
temp++;
flag=0;
if(temp>9)
{
flag=1;
temp=temp%10;
C[i]=temp;
}else
C[i]=temp;
}
}
else
{
c=b;
temp1=b-a;
for(i=c-1;i>=temp1;i--)
{
temp=A[i-temp1]-'0'+(B[i]-'0');
if(flag)
temp++;
flag=0;
if(temp>9)
{
flag=1;
temp=temp%10;
C[i]=temp;
}else
C[i]=temp;
}
}
if(a==b)
{
if(flag)
cout<<"1";
for(i=0;i<a;i++)
cout<<C[i];
cout<<endl;
continue;
}
else
{
if(a>b)
{ temp1=a;
for(i=a-b-1;i>=0;i--)
{
temp=A[i]-'0';
if(flag)
temp++;
flag=0;
if(temp>9)
{
flag=1;
temp=temp%10;
C[i]=temp;
}else C[i]=temp;
}
}
else
{
temp1=b;
for(i=b-a-1;i>=0;i--)
{
temp=B[i]-'0';
if(flag)
temp++;
flag=0;
if(temp>9)
{
flag=1;
temp=temp%10;
C[i]=temp;
}
else C[i]=temp;
}
}
if(flag)
cout<<"1";
for(i=0;i<temp1;i++)
{
// if(flag==0&&C[i]==0&&i==0)continue;
cout<<C[i];
}
cout<<endl;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -