0

0

递归程序打印所有小于N的仅由数字1或3组成的数字

王林

王林

发布时间:2023-08-29 23:05:06

|

1474人浏览过

|

来源于tutorialspoint

转载

递归程序打印所有小于n的仅由数字1或3组成的数字

We are given an integer variable as N storing the positive integer type value. The task is to recursively print all the numbers less than given value N having digit 1, 3 or the combination of both.

Let us see various input output scenarios for this −

Input − int num = 40

Output − Recursive program to print all numbers less than N which consist of digits 1 or 3 only are: 33 31 13 11 3 1

Explanation − we are given a positive integer value as 40 stored in a variable num. Now, we will recursively find out all the numbers containing digits 1, 3 or both and those numbers less than 40 are 1, 3, 11, 13, 31, 33

Input − int num = 5

Output − Recursive program to print all numbers less than N which consist of digits 1 or 3 only are: 3 1

Explanation − we are given a positive integer value as 40 stored in a variable num. Now, we will recursively find out all the numbers containing digits 1, 3 or both and those numbers less than 5 are 1 and 3.

Input − int num = 1

Output − Wrong Input

Explanation − 我们给定了一个存储在变量num中的正整数值为1。现在,我们将递归地找出所有包含数字1、3或两者的数字,并且这些数字小于1 are 0 because the only positive integer less than 1 is 0 therefore, output is wrong input.

Approach used in the below program is as follows

  • Input an integer variable as num. Pass it to the function Recursive_Numbers(num) by passing num as the parameter to the function.

    新力企业站
    新力企业站

    我们的目标:麻雀虽小,五脏俱全!致力于打造互联网上程序最小功能齐全的网站源码,只要你会打字就会做网站和管理网站。任何个人和组织不得用于商业用途,本网站专业为你订做网站。1.本网站程序是基于asp 上的,本程序由新力完成,版权归新力所有.2.本网站程序功能齐全,功能强大!3.本网站程序可符合百度谷歌更新标准。4.本网站程序模板可以导入,导出,便于快速更新模板。5.本网站程序适合初学者到程序高手都可以

    下载
  • Inside the function Recursive_Numbers(num)

    • Declare a variable as check of type bool and set it with 1.

    • Check IF num greater than 0 then start WHILE temp greater than 0 AND check to 1. Set digit to temp % 10.

    • Check IF digit not equals 1 AND digit not equals to 3 then set check to 0. Set temp = temp / 10.

    • Check IF check is 1 then print num.

    • Make a recursive call to the function Recursive_Numbers(num - 1)

Example

#include 
using namespace std;
void Recursive_Numbers(int num){
   bool check = 1;
   int temp = num;
   if(num > 0){
      while(temp > 0 && check == 1){
         int digit = temp % 10;
         if (digit != 1 && digit != 3){
            check = 0;
         }
         temp = temp / 10;
      }
      if(check == 1){
         cout<< num << " ";
      }
      Recursive_Numbers(num - 1);
   }
}
int main(){
   int num = 40;
   if(num <= 1){
      cout<<"Wrong input";
   }
   else{
      cout<<"Recursive program to print all numbers less than N which consist of digits 1 or 3 only are: ";
      Recursive_Numbers(num);
   }
   return 0;
}

输出

如果我们运行上述代码,将会生成以下输出

Recursive program to print all numbers less than N which consist of digits 1 or 3 only are: 33 31
13 11 3 1

相关文章

全能打印神器
全能打印神器

全能打印神器是一款非常好用的打印软件,可以在电脑、手机、平板电脑等设备上使用。支持无线打印和云打印,操作非常简单,使用起来也非常方便,有需要的小伙伴快来保存下载体验吧!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Sass和less的区别
Sass和less的区别

Sass和less的区别有语法差异、变量和混合器的定义方式、导入方式、运算符的支持、扩展性等。本专题为大家提供Sass和less相关的文章、下载、课程内容,供大家免费下载体验。

198

2023.10.12

python中print函数的用法
python中print函数的用法

python中print函数的语法是“print(value1, value2, ..., sep=' ', end=' ', file=sys.stdout, flush=False)”。本专题为大家提供print相关的文章、下载、课程内容,供大家免费下载体验。

184

2023.09.27

if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

713

2023.08.22

while的用法
while的用法

while的用法是“while 条件: 代码块”,条件是一个表达式,当条件为真时,执行代码块,然后再次判断条件是否为真,如果为真则继续执行代码块,直到条件为假为止。本专题为大家提供while相关的文章、下载、课程内容,供大家免费下载体验。

82

2023.09.25

string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

312

2023.08.02

int占多少字节
int占多少字节

int占4个字节,意味着一个int变量可以存储范围在-2,147,483,648到2,147,483,647之间的整数值,在某些情况下也可能是2个字节或8个字节,int是一种常用的数据类型,用于表示整数,需要根据具体情况选择合适的数据类型,以确保程序的正确性和性能。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

522

2024.08.29

c++怎么把double转成int
c++怎么把double转成int

本专题整合了 c++ double相关教程,阅读专题下面的文章了解更多详细内容。

49

2025.08.29

C++中int的含义
C++中int的含义

本专题整合了C++中int相关内容,阅读专题下面的文章了解更多详细内容。

190

2025.08.29

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

145

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

相关下载

更多

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Java 教程
Java 教程

共578课时 | 40.9万人学习

Vue.js 微实战--十天技能课堂
Vue.js 微实战--十天技能课堂

共18课时 | 1.1万人学习

PHP基础入门课程
PHP基础入门课程

共33课时 | 1.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号