热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

打印所有小于或等于N的强数字

打印所有小于或等于 N 的强数字原文:https://www . geesforgeks . org/print-all-str

打印所有小于或等于 N 的强数字

原文:https://www . geesforgeks . org/print-all-strong-numbers-小于或等于 n/

给定一个数字 N ,打印所有小于或等于 N 的强数字。

强数是一个特殊数,其位数的阶乘之和等于原数。
比如:145 是强号。从,1!+ 4!+ 5!= 145.

示例:

输入: N = 100
输出: 1 2
说明:
只有 1 和 2 是 1 到 100 的强号,因为
1!= 1,
2!= 2

输入: N = 1000
输出: 1 2 145
说明:
只有 1、2、145 是 1 到 1000 的强号,因为
1!= 1,
2!= 2,
(1!+ 4!+ 5!) = 145

方法:思路是从【1,N】迭代,检查范围内是否有强数。如果是,则打印相应的号码,否则检查下一个号码。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include
using namespace std;
// Store the factorial of all the
// digits from [0, 9]
int factorial[] = { 1, 1, 2, 6, 24, 120,
                    720, 5040, 40320, 362880 };
// Function to return true
// if number is strong or not
bool isStrong(int N)
{
    // Converting N to String so that
    // can easily access all it's digit
    string num = to_string(N);
    // sum will store summation of
    // factorial of all digits
    // of a number N
    int sum = 0;
    for(int i = 0; i     {
        sum += factorial[num[i] - '0'];
    }
    // Returns true of N is strong number
    return sum == N;
}
// Function to print all
// strong number till N
void printStrongNumbers(int N)
{
    // Iterating from 1 to N
    for(int i = 1; i <= N; i++)
    {
        // Checking if a number is
        // strong then print it
        if (isStrong(i))
        {
            cout <        }
    }
}
// Driver Code
int main()
{
    // Given number
    int N = 1000;
    // Function call
    printStrongNumbers(N);
    return 0;
}
// This code is contributed by rutvik_56

Java 语言(一种计算机语言,尤用于创建网站)

// Java program for the above approach
class GFG {
    // Store the factorial of all the
    // digits from [0, 9]
    static int[] factorial = { 1, 1, 2, 6, 24, 120,
                               720, 5040, 40320, 362880 };
    // Function to return true
    // if number is strong or not
    public static boolean isStrong(int N)
    {
        // Converting N to String so that
        // can easily access all it's digit
        String num = Integer.toString(N);
        // sum will store summation of
        // factorial of all digits
        // of a number N
        int sum = 0;
        for (int i = 0;
             i             sum += factorial[Integer
                                 .parseInt(num
                                               .charAt(i)
                                           + "")];
        }
        // Returns true of N is strong number
        return sum == N;
    }
    // Function to print all
    // strong number till N
    public static void
    printStrongNumbers(int N)
    {
        // Iterating from 1 to N
        for (int i = 1; i <= N; i++) {
            // Checking if a number is
            // strong then print it
            if (isStrong(i)) {
                System.out.print(i + " ");
            }
        }
    }
    // Driver Code
    public static void
        main(String[] args)
            throws java.lang.Exception
    {
        // Given Number
        int N = 1000;
        // Function Call
        printStrongNumbers(N);
    }
}

Python 3

# Python3 program for the
# above approach
# Store the factorial of
# all the digits from [0, 9]
factorial = [1, 1, 2, 6, 24, 120,
             720, 5040, 40320, 362880]
# Function to return true
# if number is strong or not
def isStrong(N):
    # Converting N to String
    # so that can easily access
    # all it's digit
    num = str(N)
    # sum will store summation
    # of factorial of all
    # digits of a number N
    sum = 0
    for i in range (len(num)):
        sum += factorial[ord(num[i]) -
                         ord('0')]
    # Returns true of N
    # is strong number
    if sum == N:
       return True
    else:
       return False
# Function to print all
# strong number till N
def printStrongNumbers(N):
    # Iterating from 1 to N
    for i in range (1, N + 1):
        # Checking if a number is
        # strong then print it
        if (isStrong(i)):
            print (i, end = " ")
# Driver Code
if __name__ == "__main__":
    # Given number
    N = 1000
    # Function call
    printStrongNumbers(N)
# This code is contributed by Chitranayal

C

// C# program for the above approach
using System;
class GFG{
// Store the factorial of all the
// digits from [0, 9]
static int[] factorial = { 1, 1, 2, 6, 24, 120,
                         720, 5040, 40320, 362880 };
// Function to return true
// if number is strong or not
public static bool isStrong(int N)
{
    // Converting N to String so that
    // can easily access all it's digit
    String num = N.ToString();
    // sum will store summation of
    // factorial of all digits
    // of a number N
    int sum = 0;
    for (int i = 0; i     {
        sum += factorial[int.Parse(num[i] + "")];
    }
    // Returns true of N is strong number
    return sum == N;
}
// Function to print all
// strong number till N
public static void printStrongNumbers(int N)
{
    // Iterating from 1 to N
    for (int i = 1; i <= N; i++)
    {
        // Checking if a number is
        // strong then print it
        if (isStrong(i))
        {
            Console.Write(i + " ");
        }
    }
}
// Driver Code
public static void Main(String[] args)
{
    // Given Number
    int N = 1000;
    // Function Call
    printStrongNumbers(N);
}
}
// This code is contributed by sapnasingh4991

java 描述语言


Output: 

1 2 145

时间复杂度: O(N)


推荐阅读
  • 电话号码的字母组合解题思路和代码示例
    本文介绍了力扣题目《电话号码的字母组合》的解题思路和代码示例。通过使用哈希表和递归求解的方法,可以将给定的电话号码转换为对应的字母组合。详细的解题思路和代码示例可以帮助读者更好地理解和实现该题目。 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 怎么在PHP项目中实现一个HTTP断点续传功能发布时间:2021-01-1916:26:06来源:亿速云阅读:96作者:Le ... [详细]
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
  • Oracle分析函数first_value()和last_value()的用法及原理
    本文介绍了Oracle分析函数first_value()和last_value()的用法和原理,以及在查询销售记录日期和部门中的应用。通过示例和解释,详细说明了first_value()和last_value()的功能和不同之处。同时,对于last_value()的结果出现不一样的情况进行了解释,并提供了理解last_value()默认统计范围的方法。该文对于使用Oracle分析函数的开发人员和数据库管理员具有参考价值。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 3.223.28周学习总结中的贪心作业收获及困惑
    本文是对3.223.28周学习总结中的贪心作业进行总结,作者在解题过程中参考了他人的代码,但前提是要先理解题目并有解题思路。作者分享了自己在贪心作业中的收获,同时提到了一道让他困惑的题目,即input details部分引发的疑惑。 ... [详细]
  • 本文讨论了如何使用IF函数从基于有限输入列表的有限输出列表中获取输出,并提出了是否有更快/更有效的执行代码的方法。作者希望了解是否有办法缩短代码,并从自我开发的角度来看是否有更好的方法。提供的代码可以按原样工作,但作者想知道是否有更好的方法来执行这样的任务。 ... [详细]
author-avatar
牧羊人2602903895
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有