现在的位置: 首页 > 综合 > 正文

hdu2212 DFS

2018年02月22日 ⁄ 综合 ⁄ 共 927字 ⁄ 字号 评论关闭
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 ......
#include<stdio.h>
int f[15],an;
int pass(unsigned int sum)//判断每一位的介层和是不是等于本身,反回是几位数
{
    unsigned int su=0,a=sum;
    int k,t=0;
    while(a>0)
    {
        k=a%10;
        su+=f[k];
        a/=10;
        t++;
    }
    return (su==sum)?t:0;
}
void DFS(unsigned int sum,int k,int t)//k是sum要达到多少位数,t是当前的数是几位数
{
    int e;
    if(pass(sum)==k)//只有当反回的位数等要求的位数才输出,不然会重复
    printf("%d\n",sum);

    if(t<k)//当前的位数达不到要求的位数才继续加
    for(e=0;e<10;e++)
    if((e!=0||t!=0)&&sum*10+e<=2147483647)//(e!=0||t!=0)是控制最高位不为0
    {
        DFS(sum*10+e,k,t+1);
    }
}
int main()
{
    int i;
    f[0]=1;
    for(i=1;i<10;i++)
    f[i]=f[i-1]*i;

    for(i=1;i<6;i++)
    DFS(0,i,0);
}

抱歉!评论已关闭.