HDOJ(HDU) 2212 DFS(阶乘相关、)

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
……

其实你输出后就会知道。。输出就只有4个数,你可以直接输出。
在这里,我是写了过程的。

public class Main{
    static int fact[] = new int[10];
    public static void main(String[] args) {
        dabiao();
        //9!*7 7位数-比9999999小,后面的数字更不用说了,肯定小。
        for(int i=1;i<=9999999;i++){
            if(isTrue(i)){
                System.out.println(i);
            }
        }
    }

    private static void dabiao() {
        //求阶乘的,注意:0的阶乘为1
        fact[0]=1;
        for(int i=1;i<fact.length;i++){
            fact[i]=1;
            for(int j=1;j<=i;j++){
                fact[i]=fact[i]*j;
            }
        }
    }

    //判断是不是相等
    private static boolean isTrue(int i) {
        if(i==1||i==2){
            return true;
        }
        int sum=0;
        int n=i;
        while(n!=0){
            int k=n%10;
            sum+=fact[k];
            n=n/10;
        }
        if(sum==i){
            return true;
        }
        return false;
    }
}
时间: 2024-10-25 11:13:26

HDOJ(HDU) 2212 DFS(阶乘相关、)的相关文章

HDOJ 2212 DFS

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 numbe

阶乘相关的算法及其C++实现

       有关阶乘的算法,不外乎两个方面:一是高精度计算:二是与数论相关. 一. 高精度计算阶乘 这实际上是最没有技术含量的问题,但是又会经常用到,所以还是得编写,优化它的计算. 首先看小于等于12的阶乘计算(计算结果不会超出32位范围): int factorial(int n) { if (n == 1 || n == 0) return 1; return factorial(n-1)*n; } 这个递归程序简单明了,非常直观,然而一旦n > 12,则超过32位int型的范围出现错误结

hdu 2212

http://acm.hdu.edu.cn/showproblem.php?pid=2212 唉,只需要打出数来就行了!!! #include <iostream> #include <cstdio> using namespace std; //int jc(int m) //{ // int sum=1; // for(int i=1; i<=m; i++) // sum*=i; // return sum; //} //int dfs(int m) //{ // int

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t

HDOJ(HDU) 2061 Treasure the new start, freshmen!(水题、)

Problem Description background: A new semester comes , and the HDU also meets its 50th birthday. No matter what's your major, the only thing I want to tell you is:"Treasure the college life and seize the time." Most people thought that the colle

HDOJ(HDU) 2109 Fighting for HDU(简单排序比较)

Problem Description 在上一回,我们让你猜测海东集团用地的形状,你猜对了吗?不管结果如何,都没关系,下面我继续向大家讲解海东集团的发展情况: 在最初的两年里,HDU发展非常迅速,综合各种ACM算法生成的老鼠药效果奇好,据说该药专对老鼠有效,如果被人误食了,没有任何副作用,甚至有传闻说还有健胃的效果,不过这倒没有得到临床验证.所以,公司的销量逐年递增,利润也是节节攀升,作为股东之一的公主负责财务,最近半年,她实在辛苦,多次因为点钞票造成双手抽筋而住院,现在在她面前你根本不要提到"

HDOJ(HDU) 2107 Founding of HDU(找最大值)

Problem Description 经过慎重的考虑,XHD,8600, LL,Linle以及RPG等ACM队员集体退役,甚至正在酝酿退学. 为什么?要考研?那也不用退学呀- 当然不是!真正的原因是他们想提前创业,想合伙成立一家公司,据说公司的名称都想好了,为了感谢多年的ACM集训队队长XHD,公司就叫海东集团(HaiDong Union),简称HDU.(对于这个公司名称,几个人私下里开玩笑说,外面的人看到HDU,可别以为是"胡捣集团",呵呵) 公司成立了,谁来做老大呢?这对于合伙的

HDOJ(HDU) 2500 做一个正气的杭电人(水~)

Problem Description 做人要有一身正气,杭电学子都应该如此.比如我们今天的考试就应该做到"诚信"为上. 每次考试的第一个题目总是很简单,今天也不例外,本题是要求输出指定大小的"HDU"字符串,特别地,为了体现"正气"二字,我们要求输出的字符串也是正方形的(行数和列数相等). Input 输入的第一行包含一个正整数N(N<=20),表示一共有N组数据,接着是N行数据,每行包含一个正整数M(M<=50),表示一行内有M个