little_q_exist @ 2024-10-23 20:33:57
样例都妹过(泪
#include<iostream>
#include<cstring>
using namespace std;
int a[52],b[52],c[102],d[52],f[102];
int len_a = 0,len_b = 1,len_c = 1,len_d = 1;
int main(){
int n;
b[0] = 1;
cin>>n;
for (int i = 1;i<=n;i++)
{
int p = i;
while (p>0) //采用在i-1的结果上累乘i
{
a[len_a] = p%10;//把i输入a数组
p /= 10;
len_a ++;
}
for (int i = 0;i<len_a;i++)
{
for (int j = 0;j<len_b;j++)
{
c[i+j] += a[i]*b[j];//高精乘
}
}
while (c[len_c]) len_c ++;
for (int i = 0;i<len_c;i++)
{
if (c[i] > 9)//c数组中存结果
{
c[i+1] += c[i]/10;
c[i] %= 10;
}
}
while (c[len_c]) len_c ++;
len_b = len_c;//结果丢到b数组
for (int i = 0;i<len_b;i++)
{
b[i] = c[i];
}
//同时进行高精加法
for (int i = 0;i<len_c;i++)
{
d[i] += c[i];
}
while (d[len_d]) len_d ++;
for (int i = 0;i<len_d;i++)
{
if (d[i] > 9)
{
d[i+1] += d[i]/10;
d[i] %= 10;
}
}
while (d[len_d]) len_d ++;
}
for (int i = len_d - 1;i>=0;i--)
{
cout<<d[i];
}
cout<<endl;
return 0;
}
by 佳睿2012 @ 2024-10-23 20:41:11
应是c[i+j+1]=a[i] * b[j]
by geyuqian @ 2024-11-04 19:05:59
可以看看我用c写的
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define MAX_BIT 2000
//大数加法
void num_add(int* sum, int* num)
{
int i = 0;
int flag = 0;
for (i = 0; i < MAX_BIT; i++) {
sum[i] += num[i] + flag;
flag = sum[i] / 10;
sum[i] %= 10;
}
}
//大数乘法
void num_multiply(int* num, int x)
{
int flag = 0;
int i = 0;
for (i = 0; i < MAX_BIT; i++) {
num[i] =num[i]* x + flag;
flag = num[i] / 10;
num[i] %= 10;
}
}
//计算阶乘
void result(int n)
{
int i = 0;
int tmp[MAX_BIT];
int ret[MAX_BIT];
memset(ret, 0, sizeof(ret));
memset(tmp, 0,sizeof(tmp));
tmp[0] = 1;
for (i = 1;i <= n; i++) {
num_multiply(tmp, i);
num_add(ret, tmp);
}
for (i = MAX_BIT - 1; i >= 0; i--) {
if (ret[i] != 0) {
break;
}
}
for (; i >=0 ; i--) {
printf("%d", ret[i]);
}
}
int main()
{
int input = 0;
scanf("%d", &input);
result(input);
return 0;
}