Factorial and time limit: Ms | Memory limit: 65535 KB Difficulty: 3
-
Describe
-
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
-
2910
-
Sample output
-
YesNo
Note the case of the OUTPUT results!!!!!
#include <stdio.h> #include <math.h>int main () {int n,m,j,i,t,sum; int a[12];scanf ("%d", &n), while (n--) {sum=1;t=1;for (i=1;i<=10;i++) {for (j=1;j<=i;j++) ///Because the maximum number is 1 million, The factorial of 10 is greater than 1 million { //So the factorial of these numbers to 9 is enough sum*=j; This loop puts the factorial of 1 to 9 into the array a}a[t]=sum;t++;sum=1;} scanf ("%d", &m), for (i=10;i>=1;i--) {if (M>=a[i]) m=m-a[i];//ask whether to conform to} if (m==0) printf ("yes\n"); elseprintf ("no\n"); }return 0;}
Nyoj 91 the sum of factorial