#include <bits/stdc++.h>
using namespace std;
int Factorials(int n) {
if (n == 0) return 1;
else if (n >= 1) {
int result = 1;
for (int i = n; i > 0; i--) {
result *= i;
}
return result;
} else return -1;
}
int main() {
int FacResult[10];
for (int i = 0; i < 10; i++) {
FacResult[i] = Factorials(i);
}
int n;
while (cin >> n) {
for (int j = 9; j >= 0; j--) {
if (n >= FacResult[j]) {
n -= FacResult[j];
}
}
if (n == 0) cout << "YES" << endl;
else if (n != 0 ) cout << "NO" << endl;
}
}
才发现这题一个阶乘只能用一次?
如果一个阶乘可以用多次的话在for循环里再套个while循环一直减就可以了。

京公网安备 11010502036488号