设为首页 加入收藏

TOP

POJ数据的输入输出格式(二)
2019-06-11 10:08:09 】 浏览:147
Tags:POJ 数据 输入 输出 格式
udapest. His Ph.D. dissertation on set theory was an important contribution to the subject. At the age of 20, von Neumann proposed a new definition of ordinal numbers that was universally adopted. While still in his twenties, he made many contributions in both pure and applied mathematics that established him as a mathematician of unusual depth. His Mathematical Foundations of Quantum Mechanics (1932) built a solid framework for the new scientific discipline. During this time he also proved the mini-max theorem of GAME THEORY. He gradually expanded his work in game theory, and with coauthor Oskar Morgenstern he wrote Theory of Games and Economic Behavior (1944).

There are some numbers which can be expressed by the sum of factorials. For example 9,9=1!+2!+3! Dr. von Neumann was very interested in such numbers. So, he gives you a number n, and wants you to tell him whether or not the number can be expressed by the sum of some factorials.

Well, it's just a piece of cake. For a given n, you'll check if there are some xi, and let n equal to Σ1<=i<=txi!. (t >=1 1, xi >= 0, xi = xj iff. i = j). If the answer is yes, say "YES"; otherwise, print out "NO".

Input

You will get several non-negative integer n (n <= 1,000,000) from input file. Each one is in a line by itself.

The input is terminated by a line with a negative integer.

Output

For each n, you should print exactly one word ("YES" or "NO") in a single line. No extra spaces are allowed.

Sample Input

9

-1

Sample Output

YES

    (1)编程思路。

    由于题目要求处理的数据范围为n <= 1,000,000,而10!=3628800>1000000,因此,可以定义一个数组int table[11]预先保存好0!~10!的值。

    针对每个输入的测试数据n,用循环

        for (i =10; i>=0; i--)

            if (table[i]<=n)  n=n-table[i]; 

    进行处理,若n==0,则n可以表示为几个数的阶乘和,输出“YES”。

    (2)源程序。

#include <iostream>  

using namespace std; 

int main()

    int n,i; 

    int table[11]={1,1,2,6,24,120,720,5040,40320,362880,3628800};

    while (cin>>n && n>=0)

       { 

        if(n == 0)

              { 

            cout<<"NO"<<endl; 

            continue; 

        } 

        for (i =10; i>=0; i--)

              { 

            if (table[i]<=n)  n=n-table[i]; 

            if (n==0)   break; 

        } 

        if (i>=0)  cout<<"YES"<<endl; 

        else   cout<<"NO"<<endl; 

    }&nb

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇POJ中和质数相关的三个例题(POJ .. 下一篇上POJ刷题

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目