原文:

给定一个数字 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 < num.length(); 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 << i << " ";
        }
    }
}
// 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 < num.length(); 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 < num.length; 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)