C语言基础习题50例(六)26-30

习题26

利用递归方法求5!。

实现思路: 使用递归。

代码如下:

1#include<stdio.h> 2 3int main(){ 4 int rec(int n); 5 int result = rec(5); 6 printf("5! = %d\n", result); 7 8 return 0; 9} 10 11int rec(int n){ 12 if(n == 1 || n == 0){ 13 return 1; 14 } 15 else{ 16 return n * rec(n - 1); 17 } 18}

打印:

15! = 120 2

习题27

利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。

实现思路: 使用递归。

代码如下:

1#include<stdio.h> 2 3int main(){ 4 void rev(int n); 5 printf("Please input 5 characters:\n"); 6 rev(5); 7 8 return 0; 9} 10 11void rev(int n){ 12 char ch; 13 if(n <= 1){ 14 ch = getchar(); 15 printf("Result is as follows:\n"); 16 putchar(ch); 17 } 18 else{ 19 ch = getchar(); 20 rev(n - 1); 21 putchar(ch); 22 } 23}

打印:

1Please input 5 characters: 2acdfg 3Result is as follows: 4gfdca

习题28

有 5 个人坐在一起: 问第5个人多少岁,他说比第 4 个人大 2 岁;问第 4 个人岁数,他说比第 3 个人大 2 岁;问第3个人,又说比第 2 人大两岁;问第 2 个人,说比第1个人大两岁;最后问第1个人,他说是 10 岁。 请问第五个人多大?

实现思路: 可以简单倒推、使用循环即可。 这里为了熟悉递归的思想,因此采用递归方法实现: 要想知道第5个人岁数,需知道第四人的岁数,依次类推,推到第1人(10岁),再往回推。

代码如下:

1#include<stdio.h> 2 3int main(){ 4 int age(int n); 5 int a = age(5); 6 printf("The age of the 5th person is %d\n", a); 7 8 return 0; 9} 10 11int age(int n){ 12 if(n == 1){ 13 return 10; 14 } 15 else{ 16 return age(n - 1) + 2; 17 } 18}

打印:

1The age of the 5th person is 18 2

习题29

给一个不多于 5 位的正整数,要求: 一、求它是几位数; 二、逆序打印出各位数字。

实现思路: 逐位获取并判断。

代码如下:

1#include<stdio.h> 2 3int main(){ 4 int a, b, c, d, e, num; 5 printf("Please enter a five digit number:\n"); 6 scanf("%d", &num); 7 a = num / 10000; 8 b = num % 10000 / 1000; 9 c = num % 1000 / 100; 10 d = num % 100 / 10; 11 e = num % 10; 12 if(a != 0){ 13 printf("This number is five digits\nThe reversed num is %d, %d, %d, %d, %d\n", e, d, c, b, a); 14 } 15 else if(b != 0){ 16 printf("This number is four digits\nThe reversed num is %d, %d, %d, %d\n", e, d, c, b); 17 } 18 else if(c != 0){ 19 printf("This number is three digits\nThe reversed num is %d, %d, %d\n", e, d, c); 20 } 21 else if(d != 0){ 22 printf("It's a two digit number\nThe reversed num is %d, %d\n", e, d); 23 } 24 else{ 25 printf("This number is one digit\nThe reversed num is %d\n", e); 26 } 27 28 return 0; 29}

打印:

1Please enter a five digit number: 219537 3This number is five digits 4The reversed num is 7, 3, 5, 9, 1 5

习题30

一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

实现思路: 获取每一位的数,并比较判断。

代码如下:

1#include<stdio.h> 2 3int main(){ 4 int a, b, d, e, num; 5 printf("Please enter a five digit number:\n"); 6 scanf("%d", &num); 7 a = num / 10000; 8 b = num % 10000 / 1000; 9 d = num % 100 / 10; 10 e = num % 10; 11 if(a == e && b == d){ 12 printf("%d is a palindrome number\n", num); 13 } 14 else{ 15 printf("%d is not a palindrome number\n", num); 16 } 17 18 return 0; 19}

打印:

1Please enter a five digit number: 212321 312321 is a palindrome number 4

本文原文首发来自博客专栏C语言实战,由本人转发至https://www.helloworld.net/p/ZaOt0Lu7wuzB,其他平台均属侵权,可点击https://blog.csdn.net/CUFEECR/article/details/106598893查看原文,也可点击https://blog.csdn.net/CUFEECR浏览更多优质原创内容。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593

Linux查看GPU信息和使用情况

1、Linux查看显卡信息:lspci|grepivga2、使用nvidiaGPU可以:lspci|grepinvidia!(https://oscimg.oschina.net/oscnet/36e7c7382fa9fe49068e7e5f8825bc67a17.png)前边的序号"00:0f.0"是显卡的代

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid