求解问题...
输入一个整数N,这个整数能被分解成若干个整数的阶层相加..如N=x!+y!+.....+n!并且各个整数互不相等..输入N,求x+y+.....+n的值..
#include <stdio.h> #include <stdlib.h> #define MAX 10000000 int factorial(int *fa) { int i=1; int p=1; while(1) { p = p*i; if (p>MAX) break; fa[i++]=p; } return i-1; } int lit_n(int *pn,int *fa, int s) { int i; for (i=1;i<s ;i++ ) if (*pn>=fa[i] && *pn<fa[i+1]) { *pn = *pn-fa[i]; return i; } return -1; } int main() { int fa[100]; int pr[100]; int s = factorial(fa); int n,cons_n,i=0,j; scanf("%d",&n); cons_n = n; pr[0]=lit_n(&n,fa, s); while(n>0) { i++; pr[i]=lit_n(&n,fa, pr[i-1]); if (pr[i]==-1) break; } if (n==0) { printf("%d = %d!",cons_n,pr[0]); for (j=1;j<=i ;j++ ) printf(" + %d!",pr[j]); printf("\n"); } else printf("Can not decompose %d\n",cons_n); return 0; }