起始值不能定义为0,这样的话i*j=0了。以后就不能循环了
沫沫、努力学习呀!c++我来了!
#include <stdio.h> #define N 20 #define SIZE 50 int a[N][SIZE] = {{1, 1}, {1, 2}, {1, 6}}; void out(int s[]) { for (int i = 1;i <= s[0];printf("%d", s[i++])); puts(""); } void Init(int n) { int i, j = 1, temp = 0; for (i = a[n-1][0];i > 0;--i) { temp += a[n-1][i] * (n+1); a[n][j++] = temp % 10; temp /= 10; } while (temp) { a[n][j++] = temp % 10; temp /= 10; } a[n][0] = --j; for (i = 0;++i <= j;--j) temp = a[n][j], a[n][j] = a[n][i], a[n][i] = temp; printf("%2d! = ", n+1); out(a[n]); } void Add(int s[], int b[]) { int temp = 0, c[SIZE] = {0}; int k = 1, i = s[0], j = b[0]; for (;i && j;--i, --j) { temp += s[i] + b[j]; c[k++] = temp % 10; temp /= 10; } while (i) { temp += s[i--]; c[k++] = temp % 10; temp /= 10; } while (j) { temp += b[j--]; c[k++] = temp % 10; temp /= 10; } while (temp) { c[k++] = temp % 10; temp /= 10; } s[0] = --k; for (i = 1;k;s[i++] = c[k--]); } int main() { int i, b[SIZE] = {0}; for (i = 3;i < N;Init(i++)); for (i = 0;i < N;Add(b, a[i++])); printf("\n1! + 2! + ... + %d! = ", N); out(b); return 0; }