【c/c++】Sum of Factorials

题目链接

题目描述

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”.

约翰·冯·诺伊曼,生于1903年12月28日,逝于1957年2月8日,是一位匈牙利裔美国数学家,他在数学、逻辑学、量子物理学、气象学、科学、计算机和博弈论的基础上做出了重要的贡献。他以惊人的记忆力和吸收思想和解决问题的速度而闻名。1925年获得苏黎世学院化学工程学士学位,1926年获得布达佩斯大学数学博士学位,他的集论博士论文对该学科有重要贡献。冯·诺伊曼在20岁时提出了序数的新定义,并被普遍采用。在他二十多岁的时候,他在纯数学和应用数学方面都做出了许多贡献,使他成为一位具有非凡深度的数学家。他的量子力学的数学基础(1932年)为这门新科学学科建立了坚实的框架。在此期间,他还证明了博弈论的极小极大定理。他逐渐扩展了他在博弈论方面的研究,并与合著者奥斯卡·摩根斯特恩合著了《博弈论与经济行为》(1944年)。有一些数可以用阶乘的和来表示。例如9,9 = 1!+ 2 !+ 3 !。冯·诺伊曼博士对这些数字非常感兴趣。所以,他给你一个数字n,让你判断这个数字是否可以由阶乘的和来表示。这只是个例子,对于一个给定的n,你会检查是否有一些xi,使得n等于Σt(上标)i = 1(下标)xi!(t≥1,xi≥0,xi= xj < = = > i = j ) t即Σxi!(t≥1,xi≥0,xi = xj <==> i= j) i=1。如果答案是yes,就说yes;否则,打印“NO”。

输入描述

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

您将从输入文件中得到一个非负整数n (n≤1,000,000)。

输出描述

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.

对于输入文件中的n,您应该在一行中准确地打印一个单词(“YES”或“NO”)。不允许有额外的空格。

输入样例

9
2

输出样例

YES
YES

思路

题干看似很复杂实际3/4都没用,Σt(上标)i = 1(下标)xi!(t≥1,xi≥0,xi= xj < = = > i = j ) t没搞懂是什么意思,不过就是判断一个数是否能用阶乘的和表示。
n上限1000000,小于10!,阶乘最多用到9!,若n大于阶乘,依次让n减去阶乘,最后如果能减完则说明能用阶乘的和表示。

代码
#include<bits/stdc++.h>
using namespace std;
//求阶乘
int fac(int n){
    if(n==0||n==1){
        return 1;
    }
    else{
        return n*fac(n-1);
    }
}

int main(){
    int n;
    int a[10];
    while(cin>>n){
        for(int i=10;i>0;i--){
            a[i] = fac(i-1);
            if(n>=a[i]){
                n-=a[i];
            }
        }
        if(n==0){
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }
    }//while
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶柖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值