文章
36
粉丝
505
获赞
55
访问
372.6k
以前写过一个n<=50的阶乘和,得用高精度
不过本题开个long long用低精写就行了
低精度写法:
#include<iostream>
using namespace std;
long long jc(int n)
{
if (n == 1)return 1;
return n * jc(n - 1);
}
int main()
{
int n;
long long sum = 0;
cin >> n;
for (int i = 1; i <= n; i++)
sum += jc(i);
cout << sum;
return 0;
}
数组高精度模拟写法:
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int c[100000], sum[100000];
int main()
{
int n;
cin >> n;
int l = 0;
c[1] = 1;
int w = 1;//位数
for(int k=1;k<=n;k++)
{
for (int i = 1; i <= w; i++)
c[i] *= k;
for (int j = 1; j <= w; j++)
if (c[j] > 9)
{
c[j + 1] += c[j] / 10;
c[j] %= 10;
if (j == w)
w++;
}
l = max(l, w);
for (int i = 1; i <=l; i++)
{
sum[i] += c[i];
}
for (int i = 1; i <= l; i++)
if (sum[i] > 9)
{
sum[i + 1] += sum[i] / 10;
sum[i] %= 10;
...
登录后发布评论
暂无评论,来抢沙发