题目链接
题意:
多组数据输入也就是C++中的:
int n;
while (cin >> n)
{
代码块
}
对于每个数据输出其阶乘的各位上的数字之和。大眼一看,没有思路,那就百度把。
百度解法:
我们进行大表,队1到1000每个数的阶乘进行计算,然后存储到一个sum数组中去。眨眼一看,好像阶乘求不到1000啊!没有关系,我们用一个很长的数组进行存储,让后依次对每位进行进制转换,然后统计每位数的和。
a[1][1] = 1;
sum[1] = 1;
for (int i = 2; i <= 1000; i++)
{
for (int j = 1; j <= 3000; j++)
{
a[i][j] = a[i - 1][j] * i;
}
for (int j = 1; j <= 3000; j++)
{
if (a[i][j] > 9)
{
a[i][j + 1] += (a[i][j] / 10);
a[i][j] = a[i][j] % 10;
}
}
for (int j = 1; j <= 3000; j++)
{
sum[i] += a[i][j];
}
}
那么,这道题目也就到此为止了:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 10000;
int a[4000][N], sum[N];
signed main()
{
a[1][1] = 1;
sum[1] = 1;
for (int i = 2; i <= 1000; i++)
{
for (int j = 1; j <= 3000; j++)
{
a[i][j] = a[i - 1][j] * i;
}
for (int j = 1; j <= 3000; j++)
{
if (a[i][j] > 9)
{
a[i][j + 1] += (a[i][j] / 10);
a[i][j] = a[i][j] % 10;
}
}
for (int j = 1; j <= 3000; j++)
{
sum[i] += a[i][j];
}
}
int n;
while (cin >> n)
{
cout << sum[n] << endl;
}
return 0;
}
标签:10,Love,int,Big,sum,long,Numbers,阶乘 From: https://www.cnblogs.com/msluli/p/16830561.html