My Carelessness

Back to your page!


Or leave your comments here.

RegendLa

导航

递归的初步应用

/***********************************************
有n级台阶,每次可以走1、2或3级,求共有多少种走法 
***********************************************/
#include <stdio.h>
int Ways(int n);
int main()
{
	int n;
	int res;
	scanf("%d",&n);
	res=Ways(n);
	printf("%d\n",res);
	return 0;
}
int Ways(int n)//利用递归 
{
	return n>=3?Ways(n-1)+Ways(n-2):n;
}

 

/**************************
输入n,求1到n的所有整数的和 
**************************/
#include <stdio.h>
int sumBy(int n);//用递归解决1到n的和的问题 
int main()
{
	int n;
	int ans;
	scanf("%d",&n); 
	ans=sumBy(n);
	printf("%d\n",ans);
	return 0;
}
int sumBy(int n)
{
	return n==1?1:sumBy(n-1)+n;
}

 

#include <stdio.h>
int fibona(int n);//求fibonacci数列第n项
int main()
{
	int n;
	int th;
	scanf("%d",&n);
	th=fibona(n);
	printf("第n项:%d\n前n项的和:%d\n",th,sum);
	return 0;
}
int fibona(int n)
{
	return n>=3?fibona(n-1)+fibona(n-2):n;
}

 

posted on 2015-02-05 11:16  最爱七  阅读(149)  评论(0编辑  收藏  举报




Thanks for your coming!
If what you read helps,I would appreciate!