时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:1845

解决:780

题目描述:

John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions to the foundations of mathematics, logic, quantum physics, meteorology, science, computers, and game theory. He was noted for a
phenomenal memory and the speed with which he absorbed ideas and solved problems. In 1925 he received a B.S. diploma in chemical engineering from Zurich Institute and in 1926 a Ph.D. in mathematics from the University of Budapest, His Ph.D. dissertation on
set theory was an important contributions 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 Foundation 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 whether or not the number can be expressed
by the sum of some factorials.

Well, it is just a piece of case. For a given n, you will check if there are some xi, and let n equal to Σt (上标) i=1(下标) xi! (t≥1, xi≥0, xi = xj <==> i = j)

           t

     即 Σ  xi! (t≥1, xi≥0, xi = xj <==> i = j)

          i=1

    If the answer is yes, say "YES"; otherwise, print out "NO".

输入:

You will get a non-negative integer n (n≤1,000,000) from input file.

输出:

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

样例输入:
9
2
样例输出:
YES
YES
来源:
2007年上海交通大学计算机研究生机试真题

思路:

题意是问给出的数是否能表示成不同阶乘的和。

由于给出的n范围较小,最大的阶乘肯定不会超过10,因此尝试从10!开始逐步到1!看是否属于n的分解式即可。

代码:

#include <stdio.h>

int main(void)
{
int n, i;
int fac[10]; while (scanf("%d", &n) != EOF)
{
if (n == 0)
{
printf("NO\n");
continue;
}
fac[0] = 1;
for (i=1; i<10; i++)
fac[i] = fac[i-1]*i;
for (i=9; i>=0; i--)
{
if (n >= fac[i])
n -= fac[i];
if (n == 0)
break;
}
if (i == -1)
printf("NO\n");
else
printf("YES\n");
} return 0;
}
/**************************************************************
Problem: 1038
User: liangrx06
Language: C
Result: Accepted
Time:0 ms
Memory:912 kb
****************************************************************/

九度OJ 1038:Sum of Factorials(阶乘的和) (DP、递归)的更多相关文章

  1. 【九度OJ】题目1179:阶乘 解题报告

    [九度OJ]题目1179:阶乘 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1179 题目描述: 输入n, 求y1=1!+3!+-m ...

  2. 九度OJ 1076:N的阶乘 (数字特性、大数运算)

    时间限制:3 秒 内存限制:128 兆 特殊判题:否 提交:6384 解决:2238 题目描述: 输入一个正整数N,输出N的阶乘. 输入: 正整数N(0<=N<=1000) 输出: 输入可 ...

  3. 九度OJ 1067:n的阶乘 (数字特性)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6476 解决:2471 题目描述: 输入一个整数n,输出n的阶乘 输入: 一个整数n(1<=n<=20) 输出: n的阶乘 样例 ...

  4. 九度OJ 1205 N阶楼梯上楼问题 (DP)

    题目1205:N阶楼梯上楼问题 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2817 解决:1073 题目描写叙述: N阶楼梯上楼问题:一次能够走两阶或一阶.问有多少种上楼方式. (要 ...

  5. 九度OJ 1345:XXX定律之画X (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:361 解决:157 题目描述: 给你一个n,然后让你输出F(n) 规则是这样的,F(n)的输出结果是: F(n-1)     F(n-1) ...

  6. 九度OJ 1260:珍珠项链 (字符串处理、DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:101 解决:27 题目描述: 假设有一条珍珠项链,有很多珍珠,r代表红色, b代表蓝色, w代表白色. 假设你在某一处剪开之后,你会沿着顺时 ...

  7. 九度OJ 1161:Repeater(复制器) (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1449 解决:508 题目描述: Harmony is indispensible in our daily life and no one ...

  8. 九度OJ 1040:Prime Number(质数) (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5278 解决:2180 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...

  9. 九度OJ 1035:找出直系亲属 (二叉树、递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2380 解决:934 题目描述:     如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外) ...

随机推荐

  1. IT人为了自己父母和家庭,更得注意自己的身体和心理健康

    我前一阵在一家互联网公司,工作节奏是995,忙的时候,要晚上10点才能离开公司,有时候周六还得加班.自己感觉身体状况有所下降,且听说其它一个组,在体检后多少都查出问题来,细思极恐. 有时候确实忙,那么 ...

  2. linux查看hostname以及修改hostname

    查看hostname   :   hostname 修改hostname   :   hostnamectl set-hostname  master (比如要修改为master) 修改完重启生效 : ...

  3. 聊聊、Zookeeper Linux 单服务

    关于上一篇 Zookeeper 的文章是介绍安装启动,这一篇介绍独立服务,也就是单台 Zookeeper 提供服务.首先登陆 Linux 系统,确保网络通畅.如果遇到找不到网卡 eth0 情况,可以先 ...

  4. 【java】TreeMap/HashMap的循环迭代中 keySet和entrySet和forEach方式 + map的几种迭代方式

    参考链接:https://www.cnblogs.com/crazyacking/p/5573528.html ================================== java紫色代表迭 ...

  5. [置顶] django快速获取项目所有的URL

    django快速获取项目所有的URL django1.10快速获取项目所有的URL列表,可以用于权限控制 函数如下: import re def get_url(urllist , parent='' ...

  6. linux挂载硬盘

    最近有个生产机由于缓存问题,硬盘不太够用,所以就加载了一块硬盘,下面把挂载硬盘的过程记录下,供大家参考,有问题欢迎留言! fdisk –l ------------------------------ ...

  7. javascript 数组Array排序

    var numberAry = [9,9,10,8,7,80,33,55,22]; numberAry.sort(); /*输出:10,22,33,55,7,8,80,9,9 上面的代码没有按照数值的 ...

  8. java程序如何优化--技巧总结

    http://www.douban.com/group/topic/17850695/

  9. Character set &#39;utf8mb4&#39; is not a compiled character set

    近期在一次MySQL数据迁移的过程中遭遇了字符集的问题,提示为"Character set 'utf8mb4' is not a compiled character set".即 ...

  10. 在Editplus中配置java的(带包)编译(javac)和执行(java)的方法

    配置的前提是电脑安装了JDK而且配置好了相关的环境变量(JAVA_HOME,path和classpath). 配置好后在命令行中输入javac和java验证是否配置成功: 假设出现上面的情况则说明配置 ...