题解 | #Sum of Factorials#

Sum of Factorials

https://www.nowcoder.com/practice/42cb1c7f7344466b8cb1710cb12d06b1

#include<bits/stdc++.h>
using namespace std;
int jie(int x){
    int n=x;
    if(x==0) return 1;
    while(n>1){
        n--;
        x=x*n;
    }
    
    return x;
}
int dfs(int x,int sum,int n){
    if(sum==x) return 1;
    if(n==10)
    return -1;
    n++;
    return max(dfs(x,sum+jie(n),n),dfs(x,sum,n));
}

int main(){
    int x;
    while(cin>>x){
        if(x<0) break;
        if(x==0) {cout<<"NO"<<endl;continue;}
        int sum=0;int flag=0;
        flag=dfs(x,sum,-1);
        if(flag==1)
        cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
        
    }
    
    
    
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
家里的一个表弟,25届北理工本硕,放弃了阿里和给户口的移动,考上了杭州交警了这选择诸位觉得咋样?
拿一个响亮的offe...:真的是有舍才有得,高薪,稳定,轻松,这三者不可能都要的,真的得多问问自己到底想要什么,到底想过什么样的生活,这一点特别重要,得听从内心的声音,然后就是坚定地选择和自洽,不要去美化没选择的那条路,而是要听从内心的声音去做选择。不然时间长了,还是会不开心的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务