10000以内的阶乘
来源:互联网 发布:linux chmod x 编辑:程序博客网 时间:2024/04/29 07:20
#include <iostream>#include <cmath>#include <stdio.h>using namespace std;/* 算法很水的,不过改良方向大概就是每四位算一个之类的东西了。。参考麦森数 */int getWeishu(int n){ double weishu = 0; for(int i = 2; i <= n; i++) { weishu += log10(i); } return (int)weishu + 1;}int main(){ int digit[40000] = {0}; digit[0] = 1; int n; cin >> n; int weishu = getWeishu(n); for(int i = 2; i <= n; i++) { for(int j = 0; j < weishu + 1; j++) { digit[j] *= i; } if(i % 2 == 0) { for(int j = 0; j < weishu + 1; j++) { if(digit[j] >= 10) { int ten = digit[j] / 10; digit[j + 1] += ten; digit[j] %= 10; } } } } for(int j = 0; j < weishu + 1; j++) { if(digit[j] >= 10) { int ten = digit[j] / 10; digit[j + 1] += ten; digit[j] %= 10; } } int outline = weishu; if(weishu % 50 != 0) outline = (weishu / 50 + 1) * 50; outline -= 1; for(int i = outline; i >= 0; i--) { printf("%d", digit[i]); if(i % 50 == 0) printf("\n"); } return 0;}