The sum of factorial

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.