/*
* Fibonacci Numbers(斐波纳契数列)
* 1, 1,2,3,5,8,13,21,34,55,89,144,233
* 求第n个斐波纳契数
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ctoi( char src )
{
switch( src )
{
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return 0;
}
}
void Reverse( char* des, char* src )
{
int length = 0;
while( src[length++] != 0 ){}
length--;
for( int i = 0; i < length; i++ )
{
des[i] = src[length-i-1];
}
}
int getlength( char* p, char* q )
{
int length = 0;
for( int i = 0; p[i] != 0; i++ ){}
length = i;
for( i = 0; q[i] != 0; i++ ){}
return length > i ? length : i;
}
void add( char* des, char* src1, char* src2 )
{
char temp1[50];
char temp2[50];
char temp3[50];
for( int i = 0; i < 50; i++ )
temp1[i] = 0;
for( i = 0; i < 50; i++ )
temp2[i] = 0;
for( i = 0; i < 50; i++ )
temp3[i] = 0;
Reverse( temp1, src1 );
Reverse( temp2, src2 );
int p = 0;
int temp;
int length = getlength( temp1, temp2 );
for( i = 0; i < length; i++ )
{
temp = ctoi( temp1[i] ) + ctoi( temp2[i] ) + p;
if( temp > 9 )
{
itoa( temp - 10, &temp3[i], 10 );
p = 1;
}
else
{
itoa( temp, &temp3[i], 10 );
p = 0;
}
}
if( p != 0 )
itoa( p, &temp3[i], 10 );
Reverse( des, temp3 );
}
void main()
{
char m[50];
char n[50];
char sum[50];
int num ;
for( int i = 0; i < 50; i++ )
m[i] = 0;
for( i = 0; i < 50; i++ )
n[i] = 0;
for( i = 0; i < 50; i++ )
sum[i] = 0;
m[0] = '1';
n[0] = '1';
i = 0;
while( scanf( "%d", &num ) != EOF )
{
while( i < num - 2 )
{
add( sum, m, n );
strcpy( n, m );
strcpy( m, sum );
i++;
}
printf( "%s\n", sum );
}
}
posted on 2007-03-04 10:15
周Q 阅读(1196)
评论(0) 编辑 收藏 引用