HOJ 1302 Sum of Factorials

http://acm.hit.edu.cn/hoj/problem/view?id=1302

问一个数能否被阶乘的和表示

0!=1

The input is terminated by a line with a negative integer.

#include 

int f(int x)
{
    int i, pro = 1;
    if(x == 0)
        return pro;
    else
        for(i = 1; i <= x; i++)
            pro *= i;
    return pro;
}
int main()
{
    int n;
    int i;
    int fac[10];

    for(i = 0; i < 10; i++)
        fac[i] = f(i);

    while (scanf("%d", &n) != EOF)
    {
        if (n < 0)
            break;
        if (n == 0)
            printf("NO\n");
        else
        {
            for (i = 9; i >= 0; i--)
            {
                if (n >= fac[i])
                    n -= fac[i];
            }
            if(n == 0)
                printf("YES\n");
            else
                printf("NO\n");
        }

    }

    return 0;
}


 

你可能感兴趣的:(HOJ)