HDOJ 2212 DFS

简介: Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every digit of a positive integer.

Problem Description
A DFS(digital factorial sum) number is found by summing the factorial of every digit of a positive integer.

For example ,consider the positive integer 145 = 1!+4!+5!, so it’s a DFS number.

Now you should find out all the DFS numbers in the range of int( [1, 2147483647] ).

There is no input for this problem. Output all the DFS numbers in increasing order. The first 2 lines of the output are shown below.

Input
no input

Output
Output all the DFS number in increasing order.

Sample Output
1
2
……

分析:9的阶乘为362880, 9!*10 而且由0~9的阶乘组成的最大数就是3628800。
而且0的阶乘是1,而不是0.
因为根据阶乘定义 n!=n*(n-1)!;
1!=1*0!=1;
所以人为规定了0!=1;

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
int k[10]= {1,1};
void ff()
{
    int i;
    for(i = 2; i < 10; i ++){
        k[i] = k[i-1]*i;
      //  printf("%d\n",k[i]);
    }
}
int main()
{
    ff();
    long i;
    long a,sum;
    for(i=1; i<=3628800; i++)
    {
        a=i;
        sum=0;
        while(a!=0)//a>0
        {
            sum+=k[a%10];
            a=a/10;
            //printf("%d\n",a);
        }
        if(sum==i)
            printf("%ld\n",i);
    }
    return 0;
}
目录
相关文章
|
6月前
|
算法
DFS算法的实现
DFS算法的实现
100 3
|
8月前
|
算法
DFS算法及应用(一)
DFS(深度优先搜索)是一种图遍历算法,常用于解决穷举问题,如全排列、迷宫问题、图的连通性等。它沿着树的深度分支进行探索,直至达到叶子节点,若无法继续则回溯。例如,将数字6拆分为3个正整数的递增序列问题可以通过DFS实现,类似地,分糖果问题和买瓜问题同样可以用DFS求解。DFS通常涉及递归或栈结构,通过标记已访问节点避免重复。在编程中,会定义递归函数,设定结束条件,然后枚举可能的情况,并处理下一层节点。
|
8月前
|
算法 索引
DFS算法及应用(二)
回溯:回溯就是DFS的一种,在搜索探索过程中寻找问题的解,当发现不满足求解条件时,就回溯返回,尝试其他路径。
|
机器学习/深度学习 人工智能 定位技术
|
C语言 C++
【DFS练习】素数环
【DFS练习】素数环
146 0
|
定位技术
DFS:踏青
DFS:踏青
|
算法
迭代加深(DFS)
复习acwing算法提高课的内容,本篇为讲解算法:迭代加深,关于时间复杂度:目前博主不太会计算,先鸽了,日后一定补上。
164 0
迭代加深(DFS)
dfs
题目描述 你有一张某海域NxN像素的照片,"."表示海洋、"#"表示陆地,如下所示: ....... .##.... .##.... ....##. ..####. ...###. ....... 其中"上下左右"四个方向上连在一起的一片陆地组成一座岛屿。例如上图就有2座岛屿。 由于全球变暖导致了海面上升,科学家预测未来几十年,岛屿边缘一个像素的范围会被海水淹没。具体来说如果一块陆地像素与海洋相邻(上下左右四个相邻像素中有海洋),它就会被淹没。 例如上图中的海域未来会变成如下样子: ....... ....... ....... ....... ....#.. ......