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

习题26

利用递归方法求5!。

实现思路: 使用递归。

代码如下:

#include<stdio.h> int main(){ int rec(int n); int result = rec(5); printf("5! = %d\n", result); return 0; } int rec(int n){ if(n == 1 || n == 0){ return 1; } else{ return n * rec(n - 1); } }

打印:

5! = 120

习题27

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

实现思路: 使用递归。

代码如下:

#include<stdio.h> int main(){ void rev(int n); printf("Please input 5 characters:\n"); rev(5); return 0; } void rev(int n){ char ch; if(n <= 1){ ch = getchar(); printf("Result is as follows:\n"); putchar(ch); } else{ ch = getchar(); rev(n - 1); putchar(ch); } }

打印:

Please input 5 characters: acdfg Result is as follows: gfdca

习题28

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

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

代码如下:

#include<stdio.h> int main(){ int age(int n); int a = age(5); printf("The age of the 5th person is %d\n", a); return 0; } int age(int n){ if(n == 1){ return 10; } else{ return age(n - 1) + 2; } }

打印:

The age of the 5th person is 18

习题29

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

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

代码如下:

#include<stdio.h> int main(){ int a, b, c, d, e, num; printf("Please enter a five digit number:\n"); scanf("%d", &num); a = num / 10000; b = num % 10000 / 1000; c = num % 1000 / 100; d = num % 100 / 10; e = num % 10; if(a != 0){ printf("This number is five digits\nThe reversed num is %d, %d, %d, %d, %d\n", e, d, c, b, a); } else if(b != 0){ printf("This number is four digits\nThe reversed num is %d, %d, %d, %d\n", e, d, c, b); } else if(c != 0){ printf("This number is three digits\nThe reversed num is %d, %d, %d\n", e, d, c); } else if(d != 0){ printf("It's a two digit number\nThe reversed num is %d, %d\n", e, d); } else{ printf("This number is one digit\nThe reversed num is %d\n", e); } return 0; }

打印:

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

习题30

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

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

代码如下:

#include<stdio.h> int main(){ int a, b, d, e, num; printf("Please enter a five digit number:\n"); scanf("%d", &num); a = num / 10000; b = num % 10000 / 1000; d = num % 100 / 10; e = num % 10; if(a == e && b == d){ printf("%d is a palindrome number\n", num); } else{ printf("%d is not a palindrome number\n", num); } return 0; }

打印:

Please enter a five digit number: 12321 12321 is a palindrome number

本文原文首发来自博客专栏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