九度OJ 1038:Sum of Factorials(阶乘的和) (DP、递归)-程序员宅基地

时间限制: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
****************************************************************/


转载于:https://www.cnblogs.com/liangrx06/p/5083993.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30852451/article/details/97946229

智能推荐

java小问题-程序员宅基地

1. 下载安装JDK并配置环境变量(1)从官网http://www.oracle.com/technetwork/java/javase/downloads/index.html下载对应Windows x64的JDK安装包。(2)在C:\Program Files\Java\jdk1.8.0_91\bin目录下存放Java开发工具。依次点击计算机&gt;属性&gt;高级系统设置&gt;环境变量,新...

SpringData+JPA+mysql 8,数据库总是自动创建 hibernate_sequence 表_spring data jpa 生成hibernate_sequence-程序员宅基地

SpringData+JPA mysql数据库总是自动创建 hibernate_sequence 表解决办法:把GeneratedValue(strategy = GenerationType.AUTO)改为@GeneratedValue(strategy = GenerationType.IDENTITY)..._spring data jpa 生成hibernate_sequence

优惠券系统:编写Ribbon应用Controller_优惠劵controller怎么写-程序员宅基地

package com.zcw.coupon.controller;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.an..._优惠劵controller怎么写

Ubuntu 18.04安装vscode_vscode安装 ubantu18.04_CodeAllen嵌入式的博客-程序员宅基地

Ubuntu 18.04安装vscode现在的Ubuntu还是比较友好的,很多软件都是图像界面安装,当然看家本事还是源码安装,下面主要说下界面安装的方法正文首先打开Ubuntu软件中心,搜索vscode进入,点击安装即可..._vscode安装 ubantu18.04

nginx命令大全-程序员宅基地

nginx -s reopen #重启Nginxnginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启Nginxnginx -s stop #强制停止Nginx服务nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)nginx -t #检测配置文件是否有语法错误,然后退出ng...

随便推点

centos7安装Docker_@猿程序的博客-程序员宅基地

安装Docker环境准备1.需要一点点的Linux基础2.Centos73.使用xshell连接远程服务器环境查看# 系统内核3.10以上[root@ajie /]# uname -r3.10.0-862.14.4.el7.x86_64# 系统版本[root@ajie /]# cat /etc/os-releaseNAME="CentOS Linux"VERSION="7 (Core)"ID="centos"ID_LIKE="rhel fedora"VERSION_I_安装docker

[字符串][第二阶段-字符串处理][HDOJ-2031]A + B Again-程序员宅基地

Problem DescriptionThere must be many A + B problems in our HDOJ , now a new one is coming.Give you two hexadecimal integers , your task is to calculate the sum of them,and print it in hexadecimal

POI 百万数据导出-程序员宅基地

POI 百万数据导出 poi 导出主类package test;import java.io.File;import java.io.FileOutputStream;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;..._poi 100数据导出

QML Shapes模块中渐变的使用简例_龚建波的博客-程序员宅基地

才发现 Qt 文档中 Shapes 模块的三种渐变没有参考代码,只在官方示例里有。其中的 LinearGradient 的使用还有点区别,得指定起止点位置才有渐变效果,我想是因为 ShapePath 自身是没有宽高属性的,所以没有默认的起止点;并且其渐变方向也是根据起止点来确定的。效果和代码如下://Circle.qml 定义一个圆形的shape组件import QtQuick 2.12import QtQuick.Shapes 1.12//两个圆弧组成的圆Shape { i

让VB自动改变控件大小-程序员宅基地

让VB自动改变控件大小 http://edu.cn700.com 当窗体大小改变时,如何动态的改变控件的大小是许多VB 程序员头痛的事。有的人设置窗体Resizable 但却不改变控件的大小;有的人则根据控件的绝对位置与窗口大小相加减的办法来重新定...

n元线性方程组解的情况及判别准则-程序员宅基地

行列式应用--克莱姆法则1. 引入2. 总结性猜测1. 引入例1. 解线性方程组:{x1+3x2+x3=23x1+4x2+2x3=9−x1−5x2+4x3=102x1+7x2+x3=1\begin{cases}x_1+3x_2+x_3=2 \\3x_1+4x_2+2x_3=9 \\-x_1-5x_2+4x_3=10\\2x_1+7x_2+x_3=1\end{cases}⎩⎪⎪⎪⎨⎪⎪⎪⎧​x1​+3x2​+x3​=23x1​+4x2​+2x3​=9−x1​−5x2​+4x3​=102x_n元线性方程组解的情况