递归

1.什么是递归?

是指函数/过程/子程序在运行过程序中直接或间接调用自身而产生的重入现象。在计算机编程里,递归指的是一个过程:函数不断引用自身,直到引用的对象已知。也就是自己调自己。

2.为什么用递归?

把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解,递归的策略只需少量的程序就可描述解题过程所需要的多次重复计算,大大减少了程序的代码量。

3.递归必要条件:

>存在条件限制,当满足这个限制条件时,递归不在继续

>每次调用递归之后越来越接近这个限制条件

递归实例:

(1)递归和非递归分别实现求第n个斐波那契数。

递归方法:

#include<stdio.h>
#include<Windows.h>
int fib(int n){
	if (n<3)
		return 1;
	else return 
		fib(n - 1) + fib(n - 2);
}
int main()
{
	int n =5;
	int f = fib(n);
	printf("%d\n",f);
	system("pause");
	return 0;
}

非递归方法:

#include<stdio.h>
#include<Windows.h>
int fib(int n){
	int a = 1;
	int b = 1;
	int c = 0;
	while (n>2){
		c = a + b;
		a = b;
		b = c;
		n--;
	}
	return c;
}
int main()
{
	int n =7;
	int f = fib(n);
	printf("%d\n",f);
	system("pause");
	return 0;
}

(2)编写一个函数实现n^k,使用递归实现

递归方法:

#include<stdio.h>
#include<windows.h>
int f(int n,int k) 
{
	if(k==0)
	return 1;
	else
	return n*f(n,k-1);
}
int main()
{	int k=0;
	int n=0;
	printf("请输入数:\n");
 	scanf("%d%d",&n,&k);
 	printf("运算结果:\n");
 	printf("%d\n",f(n,k));
	system("pause");
	return 0;
}

(3)写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和,例如,调用DigitSum(1729),则应该返回1+7+2+9,它的和是19 

#include<stdio.h>
#include<windows.h>
int DigitSum(int n){
	int sum=0;
	if(n==0){
		return 0;
	}else{
		sum=n%10;
		n=n/10;
	}

	return sum+DigitSum(n);
}
int main(){
	printf("%d\n",DigitSum(1729));
	system("pause");
	return 0;
}
(3)编写一个函数reverse_string(char * string)(递归实现)
        实现:将参数字符串中的字符反向排列。
        要求:不能使用C函数库中
        的字符串操作函数。

#include<stdio.h>
#include<windows.h>
int mystrlen(char *string){
	if(*string=='\0'){
		return 0;
	}
	return 1+mystrlen(string+1);
}
void reverse_string(char *string)
{
    char temp = 0;
    int len = mystrlen(string);
    if (len > 0)
    {
        temp = string[0];         
        string[0] = string[len - 1];
        string[len - 1] = '\0';
        //递归调用,限制条件len>0 ,每次调用的变化string++;
        reverse_string(string+1);
        string[len-1] = temp;
    }
}
int main(){
	char a[]="abcdef";
	reverse_string(a);
 	printf("%s\n",a);
	system("pause");
	return 0;
}

(5)递归和非递归分别实现strlen

递归方法:

#include<stdio.h>
#include<windows.h>
int mystrlen(char *string){
	if(*string=='\0'){
		return 0;
	}
	return 1+mystrlen(string+1);
}

int main(){
	char a[]="abcdef";
 	printf("%d\n",mystrlen(a));
	system("pause");
	return 0;
}

非递归方法:

#include<stdio.h>
#include<windows.h>
int mystrlen(char *string){
	int c=0;
	while(*string!='\0'){
		c++;
		string++;
	}
	return c;
}

int main(){
	char a[]="abcdef";
 	printf("%d\n",mystrlen(a));
	system("pause");
	return 0;
}

(6)递归和非递归分别实现求n的阶乘

递归方法:

#include<stdio.h>
#include<windows.h>
int factorial(int n){
	return n>0?n*factorial(n-1):1;
}
int main(){
	int n=0;
	scanf_s("%d",&n);
 	printf("%d\n",factorial(n));
	system("pause");
	return 0;
}
非递归方法:
#include<stdio.h>
#include<windows.h>
int factorial(int n){
	int result=1;
	while(n>1)
	{
		result*=n;
		n--;
	}
	return result;
}
int main(){
	int n=0;
	scanf_s("%d",&n);
 	printf("%d\n",factorial(n));
	system("pause");
	return 0;
}

(7)递归方式实现打印一个整数的每一位

#include<stdio.h>
#include<windows.h>
void print(int n){
	if(n>9){
		print(n/10);
	}
	printf("%d ",n%10);
}
int main(){
	int n=1234;
	print(n);
	system("pause");
	return 0;
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值