标签:span sum nbsp 循环 png scan div code amp
该题的主要思路是:
设sum为总和,n为逐渐增加的阶乘,所以sum的结果是 sum+=n!,将题拆开后就变得简单,主要思路是设计一个高精度乘,放在一个高精度加的循环里,最后的出结果
需要注意代码中 a,b,c何时清空数据
代码如下:
#include<stdio.h> #include<string.h> int sum[500], sum1[500]; int a[500], b[500],c[500],d[500]; int main(void) { int n,len_a,len_b = 1,s = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { s = 0; len_b = 1; b[1] = 1; for (int j = 1; j <= i; j++) { len_a = 1; int m = j; while (m > 0) { a[len_a++] = m % 10; m /= 10; } for (int x = 1; x <= len_a; x++) { for (int y = 1; y <= len_b; y++) { c[x + y - 1] += a[x] * b[y]; } } for (int x = 1; x <= len_a + len_b; x++) { if (c[x] > 9) { c[x + 1] += c[x] / 10; c[x] %= 10; } } s = len_a + len_b; for (int x = s; c[x] == 0; x--) s--; for (int x = 1; x <= s; x++) { b[x] = c[x]; } len_b = s; memset(c, 0, sizeof(c)); memset(a, 0, sizeof(a)); } for (int i = 1; i <= s; i++) { d[i] += b[i]; } for (int i = 1; i <= s; i++) { if (d[i] > 9) { d[i + 1] += d[i] / 10; d[i] %= 10; if (i == s) s++; } } for (int i = s; i >= 1; i--) if (d[i] == 0) { s--; } else break; memset(b, 0, sizeof(b)); } for (int i = s; i >= 1; i--) { printf("%d", d[i]); } }
标签:span sum nbsp 循环 png scan div code amp
原文地址:https://www.cnblogs.com/loliconsk/p/14315129.html