闲来无事,写了斐波那契额数列,无数据长度限制
#include <iostream>
using namespace std;
char s[1000]=" ";
void bignumplus(char* num1, char* num2)
{
int num1len =0, num2len =0;
num1len = strlen(num1);
num2len = strlen(num2);
int n1 = num1len-1,n2=num2len-1;
int p =0;
for(size_t i =0;;i++)
{
if (n1 >= 0 && n2 >= 0) { s[i] = num1[n1] + num2[n2] + p - '0'; }
if (n1 >= 0 && n2 < 0) { s[i] = num1[n1] + p ; }
if (n1 < 0 && n2 >= 0) { s[i] = num2[n2] + p ; }
p = 0;
if (s[i] > '9')
{
s[i] = s[i] - 10;
p = 1;
}
n1--;
n2--;
if (n1 < 0 && n2 < 0)
{
if (n1 == n2 && p==1)
{
s[i + 1] = '1';
}
break;
}
}
for (int i = strlen(s)-1; i >= 0; i--)
{
cout << s[i];
}
return;
}
int main()
{
int n;
char ppnum[1000] = "1";
char pnum[1000] = "1";
int i,j;
int len =0;
while(cin>>n && n!=0)
{
if(n==1 || n==2)
{
for(i=0;i<n;i++)
cout << 1 << ' ' ;
cout << endl;
}
if(n>2)
{
for(i=0;i<2;i++)
cout << 1 << ' ';
for(i=2;i<n;i++)
{
bignumplus(ppnum,pnum);
cout << endl;
len = strlen(pnum);
for(j=0;j<len;j++)
ppnum[j] = pnum[j];
ppnum[len] = '\0';
len = strlen(s);
for(j=0;j<len;j++)
pnum[j] = s[len-j-1];
pnum[len] = '\0';
}
}
cout << endl;
len = strlen(s);
for(int ii =0;ii<len;ii++)
s[ii]='\0';
strcpy(pnum,"1");
strcpy(ppnum,"1");
}
}