HOJ 1302 Sum of Factorals
930 ワード
http://acm.hit.edu.cn/hoj/problem/view?id=1302
一つの数は階乗と表示されますか?
0!=1
The input is terminated by a line with a negative integer.
一つの数は階乗と表示されますか?
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
");
else
{
for (i = 9; i >= 0; i--)
{
if (n >= fac[i])
n -= fac[i];
}
if(n == 0)
printf("YES
");
else
printf("NO
");
}
}
return 0;
}