1. 程式人生 > >函式遞迴的簡單演算法

函式遞迴的簡單演算法

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int Duigui(int n){

	int sum = 0;
	int one = 1;
	int two = 1;
	if (n <= 2){
		return 1;
	}
	while (n>2){
		sum = one + two;
		one = two;
		two = sum;
		n--;
	}
	return sum;
}
int main(){
	//1+1+2+3+5+8+13+21+34+55+.............................
	//n = (n - 1) + (n-2)
	//.遞迴和非遞迴分別實現求第n個斐波那契數。
	int n = 0;
	scanf("%d", &n);
	int sum = Duigui(n);
	printf("%d\n", sum);
	system("pause");
	return 0;
}
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int fib(int n){
	if (n <= 2){
		return 1;
	}
	else{
		return fib(n - 1) + fib(n - 2);
	}
	
}
int main(){
	//1+1+2+3+5+8+13+21+34+55+.............................
	//n = (n - 1) + (n-2)
	//.遞迴和非遞迴分別實現求第n個斐波那契數。
	int n = 0;
	scanf("%d", &n);
	int sum =fib(n);
	printf("%d\n", sum);
	system("pause");
	return 0;
}
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int poweer(int n,int k){
	if (k<=0){
		return 1;
	}
	else{
		return n*poweer(n, k - 1);
	}

}


int main(){
	//.編寫一個函式實現n^k,使用遞迴實現 
	int a = 0;
	int b = 0;
	scanf("%d %d",&a,&b);
	int ret = poweer(a,b);
	printf("%d",ret);
	system("pause");
	return 0;
}
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int zong = 0;
int DigitSum(int num){
	
	if (num > 9){
	 DigitSum(num/10);
	}
	return  zong = zong + num % 10;
}

int main(){
	//寫一個遞迴函式DigitSum(n),輸入一個非負整數,返回組成它的數字之和, 
	//例如,呼叫DigitSum(1729),則應該返回1 + 7 + 2 + 9,它的和是19
	int num = 1234564;
	int a = DigitSum(num);
	printf("%d",a);
	system("pause");
	return 0;
}
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void reverse_string(char* str){
	if (*str!='\0'){
		reverse_string(str+1);
	}
	printf("%c",*(str-1));
}
int main(){
	//4. 編寫一個函式 reverse_string(char * string)(遞迴實現) 
	//實現:將引數字串中的字元反向排列。
		//要求:不能使用C函式庫中的字串操作函式。
	char  a[100] = "asdcfv";
	reverse_string(a);
	system("pause");
	return 0;
}
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int strlen(char* str){
	if (*str=='\0'){
		return 0;
	}
	else{
		return strlen(str+1)+1;
	}
}
int main(){
	//5.遞迴和非遞迴分別實現strlen 
	char arr[100] = "asdfdfdg";
	int a = strlen(arr);
	printf("%d",a);
	system("pause");
	return 0;
}
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int cout = 0;
int strlen(char* str){
	while (1){	
		if (*str!='\0'){
			str = str + 1;
			cout++;
		}
		if (*str=='\0'){
			return cout;			
		}	
	}
}
int main(){
	//5.遞迴和非遞迴分別實現strlen 
	char arr[100] = "asdfdfdg";
	int a = strlen(arr);
	printf("%d",a);
	system("pause");
	return 0;
}
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int factorial(int n){
	if (n==1){
		return 1;
	}
	else{
		return n*factorial(n - 1);
	}
}
int main(){
	//.遞迴和非遞迴分別實現求n的階乘 
	int num = 5;
	int a = factorial(num);
	printf("%d",a);
	system("pause");
	return 0;
}
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int sum = 1;
int factorial(int n){
	for (int i = 1; i <= n;i++){
		sum = sum*i;
	}
	return sum;
}
int main(){
	//.遞迴和非遞迴分別實現求n的階乘 
	int num = 5;
	int a = factorial(num);
	printf("%d",a);
	system("pause");
	return 0;
}
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void Printf(int n){
	if (n>9){
		Printf(n/10);
	}
	printf("%d  ",n%10);
}
int main(){
	//遞迴方式實現列印一個整數的每一位
	int num = 122525252;
    Printf(num);
	system("pause");
	return 0;
}