簡單的遞歸——階乘,斐波那契

本分類只是對於算法的體現或是自己簡單的理解,沒有過多的概念,個人練習。

1.階乘

#include<iostream>
using namespace std;
int factoral(int n)//階乘
{
	if(n==0)return 1;
	return n*factoral(n-1); 
} 
int main()
{
	int x;
	cin>>x;
	cout<<factoral(x);
	return 0;
 } 

2.斐波那契

#include<iostream>
using namespace std;
int fabonacci(int n)//斐波那契數列 
{
	if(n<=1)return 1;
	return fabonacci(n-1)+fabonacci(n-2);
 }
int main()
{
	int x;
	cin>>x;
	cout<<fabonacci(x);
	return 0;
 } 

 

發佈了50 篇原創文章 · 獲贊 13 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章