C语言经典例题(递归非递归斐波那契数,n^k,拆分数字,反向排列,n的阶乘,递归非递归strlen)

1.递归和非递归分别实现求第n个斐波那契数。
递归最重要的是写出n-1和n-2项,直到判断停止的位置。
非递归需要明确前几项的关系。


非递归
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int ch = 0;
	int a = 1;
	int b = 1;
	int c = 0;
	printf("请输入需要输出的斐波那契项数:\n");
	scanf("%d", &ch);
	if (ch <= 2)
	{
		printf("第%d个斐波那契数是: %d\n",ch, a);
	}
	else
	{
		int i = 0;
		for (i = 0; i <ch-2; i++)
		{

			c = a + b;
			a = b;
			b = c;
		}
		printf("第%d个斐波那契数是:%d\n", ch, c);
	}
	system("pause");
	return 0;
}


递归
int fibo(int i)
{
	int c = 1;
	if (i == 2|| i == 1)
	{
		;
	}
	else
	{
	c = fibo(i - 1) + fibo(i - 2);
	}
	return c;
}

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
	printf("请输入需要输出的斐波那契项数:\n");
	int ch = 0;
	scanf("%d", &ch);
	int ret = fibo(ch);
	printf("第 %d 项的斐波那契数为:%d\n", ch,ret);
	system("pause");
	return 0;
}

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

double Pow(int n, int k)
{
	if(k >= 0)
	{
		if (k == 0)
		{
			return	1;
		}
		if (k == 1)
		{
			return n;
		}
		return n * Pow(n, k - 1); 
	}
	else
	{
		return 1.0/(n * Pow(n, -k - 1));
	}
}


#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int n = 0;
	int k = 0;
	printf("请输入n,k的值:\n");
	scanf("%d %d", &n, &k);
	double ret = Pow(n,k);
	printf("%lf", ret);
	system("pause");
	return 0;
}
  1. 写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和, 例如,调用DigitSum(1729),则应该返回1+7+2+9,它的和是19
    除10得到少一位数的值,模10得到的是个位数。
    当被除数小于10的时候,可以作为停止条件。
DigitSum(int ch) //DigitSum(172) 9  17 2 9  1 7 2 9
{	

	if (ch <10)
	{
		return ch;
	}
	else
	{
		return  DigitSum(ch / 10) + ch % 10;
	
	}
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
	unsigned int ch = 0;
	printf("请输入一个数:\n");
	scanf("%d", &ch);
	int ret = DigitSum(ch);
	printf("%d\n", ret);
	system("pause");
	return 0;

}
  1. 编写一个函数 reverse_string(char * string)(递归实现) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void Reverse_string(char *str) {
	if (*(++str) != '\0')
	{
		Reverse_string(str);
	}
	printf("%c", *(str - 1));
}
int main() {
	char a[] = "HelloWorld";
	Reverse_string(a);
	printf("\n");
	system("pause");
	return 0;
}

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

递归

 int Strlen(char* arr)
{
	int static count = 0;
	if ( *((arr++))== '\0')
		;
	else
	{
		count++;
		Strlen(arr++);
	}
	return count;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
	char arr[] = "abcdef";
	int  ret = Strlen(arr);
	printf("%d\n", ret);
	system("pause");
	return 0;
}







非递归

int Strlen(char* arr)
{
	int count = 0;
	while (*(arr++) != '\0')
	{
		
		count++;

	}
	
	return count;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
	char arr[] = "abcdefgh";
	int  ret = Strlen(arr);
	printf("%d", ret);
	system("pause");
	return 0;
}

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

递归
factorial(int n) 
{
	if (n == 1)
	{
		return n;
}
	else
	{
		return n * factorial(n - 1);
	}

}

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int ch = 0;
	scanf("%d", &ch);
	int ret = factorial(ch);
	printf("%d", ret);
	system("pause");
	return 0;
}

非递归



int Factorial(int n)
{
	int sum = 1;
	while(n)
	{
		sum *= n;
		n--;
	}
	return sum;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int ch = 0;
	scanf("%d", &ch);
	int ret = Factorial(ch);
	printf("%d", ret);
	system("pause");
	return 0;
}

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

int Printf(int ch)
{
	int x = 0;
	if (ch < 10)
	{
		return ch;
	}
	else
	{
		x = ch % 10;
		printf("%d ", x);
		Printf(ch / 10);
		
	}

}
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int ch = 0;
	scanf("%d", &ch);
	int ret = Printf(ch);
	printf("%d", ret);
	system("pause");
	return 0;
}

心得体会:递归一定要有条件的移动,和判断结束的标志,防止出现死循环,以及栈溢出。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值