description
Give you a non-negative integer n to determine if n is a sum of the factorial of some number (these numbers are not allowed to be reused and are positive), such as 9=1! +2!+3!, if yes, output yes, otherwise output no;
Input
The first line has an integer 0<m<100, which indicates that there is an M group of test data;
Each set of test data has a positive integer n<1000000;
Output
If the condition is met, output yes, otherwise output no;
Sample input
2
9
10 Sample output
Yes
No
#include <iostream>using namespace Std;int sum;int dg (int n) {sum=sum*n;if (n!=1) return DG (n-1); return sum;} int main () {int n,m,i,a[10];for (i=1;i<10;i++)//10!>1000000{SUM=1;A[I]=DG (i);} Cin>>n;while (n--) {cin>>m;for (i=9;i>0;i--)//decrease starting from 9 to find out if you can find some number of factorial and ==n{if (m-a[i]>0) m-=a[i];else if (m-a[i]==0) {m=0;break;}} if (m==0) cout<< "Yes" <<endl;elsecout<< "No" <<ENDL;} return 0;}
The sum of factorial