转发请注明原文:http://blog.csdn.net/qq_34911465/article/details/61646367
The next is my solution to solve this problem.
#include<iostream>
using namespace std;
char a[
1000],b[
1000];
int ai[
1000],bi[
1000],ci[
1002];
int getlen(
char *a)
{
int i=
0;
for(;;i++)
if(a[i]==
'\0')
break;
return i;
}
void add()
{
int lena = getlen(a);
int lenb = getlen(b);
for(
int i =
0 ; i < lena ; i++)
{
ai[lena-i-
1]=a[i]-
'0';
}
for(
int i =
0 ; i < lenb ; i++)
{
bi[lenb-i-
1]=b[i]-
'0';
}
int lenc = (lena>lenb)?lena:lenb;
for(
int i =
0 ; i < lenc ; ci[i]=
0,i++);
for(
int i =
0 ; i < lenc ; i++)
{
ci[i]=ai[i]+bi[i]+ci[i];
if(ci[i]>=
10)
{
ci[i+
1]+=
1;
ci[i]-=
10;
}
}
if(ci[lenc]>
0)lenc++;
for(
int i =
0 ; i < lenb ; i++)
{
a[i]=b[i];
}
a[lenb]=
'\0';
for(
int i =
0 ; i < lenc ; i++)
{
b[i] = ci[lenc-i-
1]+
'0';
}
b[lenc]=
'\0';
}
int main(
int args,
char* argv[])
{
a[
0]=
'1';a[
1]=
'\0';
b[
0]=
'1';b[
1]=
'\0';
int n;
cout<<
"please input the number:";
while(
cin>>n)
{
n-=
2;
while(n>
0)
{
add();
n--;
}
cout<<
"result is: "<<b<<endl;
cout<<
"please input the next number:";
}
cout<<
"stop input the number"<<endl;
return 0;
}
Below is my test:
转载请注明原文地址: https://ju.6miu.com/read-36533.html