大整數的加法。與以前高精度算法不同的是這次要一下子讀入一個算式。然后簡便的是,這次不用再交換順序了,直接加。但是結果的判斷稍微麻煩一點,要把開頭的0都去掉才行。一下是我的代碼。哎,剛開始把自己弄得挺混亂的,后來憤怒了,就把所有代碼全刪掉重新寫了一遍。然后一次AC
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
char a[12],b[12],c[12];
char temp[25];
bool add(char *first,char *second)
{
int len1=strlen(first);
int len2=strlen(second);
first[len1]='0';
int i,j;
for(i=0;i<len2;i++)
{
second[i]-='0';
}
for(j=0;j<=len1;j++)
{
first[j]-='0';
}
for(i=0;i<len2;i++)
{
first[i]+=second[i];
}
for(i=0;i<len1;i++)
{
if(first[i]>=10)
{
first[i+1]+=first[i]/10;
first[i]%=10;
}
}
for(i=0;i<=len1;i++)
first[i]+='0';
while(first[len1]=='0')
len1--;
int len3=strlen(c);
len3--;
while(c[len3]=='0')
len3--;
if(len1!=len3)
return false;
while(len3>=0)
{
if(c[len3]!=first[len3])
return false;
len3--;
}
return true;
}
int main()
{
while(gets(temp))
{
int kk=0;
while(temp[kk]!='+')
{
a[kk]=temp[kk];
kk++;
}
a[kk]='\0';
kk++;
int kkk=0;
while(temp[kk]!='=')
{
b[kkk]=temp[kk];
kk++;kkk++;
}
b[kkk]='\0';
kk++;
kkk=0;
while(temp[kk]!='\0')
{
c[kkk]=temp[kk];
kk++;kkk++;
}
c[kkk]='\0';
int len1=strlen(a);
int len2=strlen(b);
if(len1==1&&len2==1&&a[0]=='0'&&b[0]=='0')
{
cout<<"True"<<endl;
break;
}
if(len1>len2)
if(add(a,b))
cout<<"True"<<endl;
else
cout<<"False"<<endl;
else
if(add(b,a))
cout<<"True"<<endl;
else
cout<<"False"<<endl;
}
return 0;
}
posted on 2010-08-19 16:26
崔佳星 閱讀(1097)
評論(0) 編輯 收藏 引用 所屬分類:
POJ