题解 | #Sum of Factorials#

Sum of Factorials

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

#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>

using namespace std;

const int INF = 1e6;
const int MAXN = 100;
int jiecheng[MAXN];

int N;
int MAX_cout;
bool flag;

void initConfig()
{
    MAX_cout = 0;
    flag = false;

    jiecheng[0] = 1;
    for (int i = 1; i < MAXN&& jiecheng[i-1]<=INF; i++)
    {
        jiecheng[i] = jiecheng[i - 1] * i;
        MAX_cout++;
    }
}

void DFS(int index,int sum)
{
    if (flag == true)
    {
        return;
    }
    if (index<=MAX_cout&& sum == N)
    {
         flag = true;
    }
    else if (index > MAX_cout)
    {
        return;
    }
    else
    {
        DFS(index + 1, sum);
        if (sum + jiecheng[index] <= N)
        {
            DFS(index + 1, sum + jiecheng[index]);
        }
    }
}

int main()
{
    initConfig();
    while (scanf("%d", &N) != EOF)
    {
        DFS(0, 0);
        if (flag)
        {
            cout << "YES";
        }
        else
        {
            cout << "NO";
        }
        cout<<endl;
    }


    

}

全部评论

相关推荐

offerboyyyy:之前看到降温完收到offer了的呢佬,可以签保底等
点赞 评论 收藏
分享
西南山:哥,你的技能是在报菜单吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务