/* 5.10 编写函数,求Fibonacci数列的第n项 */
#include "stdio.h"
int fibonacci(int n);void main()
{
int n;
printf("求Fibonacci数列的第n项,请输入n\n");
scanf("%d", &n); /* VC6中n要小于 ? */
printf("Fibonacci数列的第%d项为%d", n, fibonacci(n));
}int fibonacci(int n)
{
if(n==1 || n==2)
return 1;
else
return fibonacci(n-1) + fibonacci(n-2) ;}
/* 5.11 编写函数,判断一个整数是否是回文数 palindrome */
#include "stdio.h"
int palindrome(long n);void main()
{
long n;
printf("判断一个整数是否是回文数,请输入n\n");
scanf("%ld", &n);
if(palindrome( n ))
printf("%ld是回文数",n);
else
printf("%ld不是回文数",n);
}int palindrome(long n)
{
int i,bit=0;
int a[10];
while(n!=0)
{
a[bit]=n%10;
n=n/10;
bit++;
}
for(i=0; i<bit/2; i++)
{
if(a[i]!=a[bit-1-i])
return 0;
}
return 1;
} /* 5.12 编写函数,求x的n次方,n为不小于0整数 n>=0 */
#include "stdio.h"
double power(double x, int n);void main()
{
int n;
double x;
printf("求x的n次方,请输入x和n,数字间以空格隔开\n");
scanf("%lf%d", &x, &n);
printf("%lf的%d次方为%lf", x, n, power(x, n) );
}double power(double x, int n)
{
if(n==0)
return 1;
else
return (x * power(x, n-1));}
/* 5.13 编写一个函数,用递归方法求n阶勒让德多项式的值 */
#include "stdio.h"
#include "conio.h"
double P(double x, int n);
void main()
{
double x; int n;
printf("求n阶勒让德多项式,请输入x和n\n");
scanf("%lf%d",&x, &n);
printf("结果为%lf", P(x, n)) ;
getch();
}double P(double x, int n)
{
if(n==0) return 1;
if(n==1) return x;
if(n>1) return ((2*n-1)*x-P(x,n-1)-(n-1)*P(x,n-2))/n;}
标签:练习题,return,int,double,C语言,fibonacci,printf,include,参考答案
From: https://blog.51cto.com/emanlee/8246537