Program 7.(递归)

 

     递归函数,实际上就是在运行的过程中调用自己.

     举个简单例子,递归好比你手里有一把钥匙,你用你手中的钥匙打开一扇门,结果去发现前方还有一扇门,紧接着你又用钥匙打开了这扇门,然后你又看到一扇们...但是当你开到某扇门时,发现前方是一堵墙无路可走了,你选择原路返回——这就是递归

但是如果你打开一扇门后,同样发现前方也有一扇们,紧接着你又打开下一扇门...但是却一直没有碰到尽头——这就是循环。    

    下面是几道典型例题.

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

//非递归实现
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int fib(int n)
{
	int a;
	int b;
	int c;
	a = b = 1;
	while (n > 2)
	{
		n = n - 1;
		c = b;
		b = a;
		a = b + c;
	}
	return a;
}
int main()
{
	int n = 0;
	scanf("%d", &n);
	int ret= fib(n);
	printf("%d\n", ret);
	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()
{
	int n = 0;
	scanf("%d", &n);
	int b = fib(n);
	printf("%d\n", b);
	system("pause");
	return 0;
}

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

#include <stdio.h>
#include <stdlib.h>
int pow1(int n,int k)
{
	if (k > 0)
	{
		return pow1(n, k - 1)*n;
	}
	else 
		return 1;
}
int main()
{
	int n = 0;
	int k = 0;
	scanf("%d",&n);
	scanf("%d",&k);
	int j = 0;
	j = pow1(n,k);
	printf("%d^%d = %d\n", n,k,j);
	system("pause");
	return 0;
}

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

#include <stdio.h>
#include<stdlib.h>
int DigitSum(int n)
{
	int sum = 0;
	int m = 0;
	if (n != 0)
	{
		m = n % 10;
		n = n / 10;
		sum = m + DigitSum(n);
	}
	return sum;


}
int main()
{
	int n = 0;
	printf("输入一个整数: ");
	scanf("%d", &n);
	int ret = 0;
    ret=DigitSum(n);
	printf("%d\n", ret);
	system("pause");
	return 0;
}

 

4. 编写一个函数 reverse_string(char * string)(递归实现)
实现:将参数字符串中的字符反向排列。
要求:不能使用C函数库中的字符串操作函数。

#include <stdio.h>
#include <stdlib.h>
void reverse_string(char * string)
{
	if (*string == '\0')
		printf("%c", *string);
	else
	{
		reverse_string(++string);  //输出下一个字符
		printf("%c", *(--string));  //因为string已经执行完++,所以若要输出当前字符得--

	}
}
int main()
{

	char s[] = "abcdefgh";
	reverse_string(s);
	system("pause");
	return 0;
}

5.递归和非递归分别实现strlen

//函数递归实现
#include <stdio.h>
#include<stdlib.h>
int strlen1(char *s)
{
	if (*s == '\0')
	{
		return 0;
	}
	else
	{
		return strlen1(s+1) + 1;
	}
}
int main()
{
	
	char s[] = "bashhah";
		int j = 0;
		j = strlen1(s);
		printf("长度为:%d\n", j);
		system("pause");
		return 0;
	system("pause");
	return 0;
}
//非递归实现
#include <stdio.h>
#include <stdlib.h>
int strlen1(char *s)
{
	int cout = 0;
	while (*s != '\0')
	{
		cout++;
		s++;
	}
	return cout;
}
int main()
{

	char s[] = "bashhah";
	int j = 0;
	j = strlen1(s);
	printf("长度为:%d\n", j);
	system("pause");
	return 0;
}

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

//非递归实现
#include<stdio.h>
#include <stdlib.h>
int main()
{
	printf("please input number:  ");
	int n = 0;
	scanf("%d", &n);
	int i = 1;
	int ret = 1;
	for (i = 1; i <= n; i++)
	{
		ret = ret*i;

	}
	printf("阶乘是=%d\n ", ret);
	system("pause");
	return 0;
}
//递归实现
#include<stdio.h>
#include <stdlib.h>
int factorial(int n)
{
	if (n == 1)
	{
		return 1;
	}
	else
	{
		return n*factorial(n - 1);
	}
}
int main()
{
	printf("please input number:  ");
	int n = 0;
	scanf("%d", &n);
	int ret = 0;
	ret = factorial(n);
	printf("阶乘是=%d\n ", ret);
	system("pause");
	return 0;
}


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

#include <stdio.h>
#include <stdlib.h>
void display(int n)
{
	if (n < 9)
	{
		printf("%d ", n);
	}
	if (n>9)
	{
		 display(n / 10);
		 printf("%d ", n % 10);
	}
}
int main()
{
	int num = 0;
	scanf("%d", &num);
	display(num);
	system("pause");
	return 0;
}
#include <stdio.h>
#include <stdlib.h>
void display(int n)
{
	
	if (n>9)
	{
		display(n / 10);
	}
	printf("%d ", n % 10);
}
int main()
{
	int num = 0;
	scanf("%d", &num);
	display(num);
	system("pause");
	return 0;
}

通过以上例题,不难发现使用递归有几个值得注意的地方:

   1.递归一定要有出口,否则就是死递归

   2.递归的次数不能太多,否则内存溢出

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值