题解 | #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; } }