递归:1.参数字符串中的字符反向排列 2.实现strlen 3.求n的阶乘 4.实现打印一个整数的每一位

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

#include <stdio.h>
#include <stdlib.h>
void  reverse_string(char* string){
	//先让字符串到最后,再利用递归逆序输出
	if (*string != '\0'){
		//利用指针的特性,指针自加,利用递归,一直指到字符串的结束标志,再逆向输出字符串
		string++;
		reverse_string(string);
		printf("%c", *(string-1 ));
	}
	
}
int main(){
	//char string[] = { "helo word" };
	char* string = "hello word";
    reverse_string(string);
	system("pause");
	return 0;
}

结果:
在这里插入图片描述
非递归代码:

#include <stdio.h>
#include <stdlib.h>
int strlen(char* string){
	int count = 0;
	while (*string != '\0'){
		count++;
		string++;
	}
	return count;
}
void reverse_string(char* string){
	int k = strlen(string)-1,i=0,j=k;
	char temp;
	while (i < j){
		//字符串中的第一个字符与最后一个交换,第二个与倒数第二个交换
		//i,j控制字符的变动,i表示字符从前往后移动,j表示字符从后往前移动
		temp = *(string + i);
		*(string + i) = *(string + j);
		*(string + j) = temp;
		i++;
		j--;
	}
	//输出方向排列后的字符串
	for (i = 0; i <= k; i++){
		printf("%c", *(string + i));
	}

}

int main(){
	//char* string = "hello word"; 为什么不行,思考
	char string[] = "abcdef";
	reverse_string(string);
	//printf("%s\n", string);
	system("pause");
	return 0;
}

结果:

题目:
递归和非递归实现strlen
递归代码:

#include <stdio.h>
#include <stdlib.h>
int strlen(char* string){
	if (*string == '\0'){
		return 0;
	}
	return 1 + strlen(string + 1);
}
int main(){
	char* string = "hello word";
	int len=strlen(string);
	printf("%d", len);
	system("pause");
	return 0;
}

结果:
在这里插入图片描述
非递归代码:

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

int main(){
        char* string = "hello word";
		int len=strlen(string);
		printf("%d", len);
		system("pause");
		return 0;
}

题目:
递归和非递归求n的阶乘
递归代码:

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int Factorial(int n){
	if (n == 1){
		return 1;
	}
	return n*Factorial(n - 1);
}
int main(){
	int n, m;
	scanf("%d", &n);
	m = Factorial(n);
	printf("the %d factorial is %d",n, m);
	system("pause");
	return 0;
}

非递归代码:

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int Factorial(int n){
	//f保留了上次调用结束的值
	static int f = 1;
	//在上次的f值基础上再乘以n
	f = f*n;
	//返回的f是n!的值
	return f;
}
int main(){
	int i, n;
	scanf("%d", &n);
	//先后调用n次Factorial函数
	//每次计算并输出i!的值
	for (i = 1; i <= n; i++){
		printf("%d!=%d\n", i, Factorial(i));
	}
	system("pause");
	return 0;
}

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//简单的方法
int main(){
	int n, i,sum=1;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++){
		sum = sum*i;
	}
	printf("%d", sum);
	system("pause");
	return 0;
}

结果:
在这里插入图片描述
题目:
递归方法实现打印一个整数的每一位
递归代码:

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

结果:
在这里插入图片描述
非递归代码:

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(){
	//逆序输出
	int num;
	scanf("%d", &num);
	while(num != 0){
		printf("%d ",num % 10);
		num = num / 10;
	}
	system("pause");
	return 0;
}

结果:
在这里插入图片描述
结语:
如有不足请多指教。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值