c语言求助大神,有两个测试点不对,谢谢!!!

P1009 [NOIP1998 普及组] 阶乘之和

123J @ 2022-08-16 14:48:10

#include<stdio.h>
int main(){
  double n,p=0.0,sum = 0,t=1.0;
  int i;
  scanf("%ld",&n);
  for(int i=1;i<=n;i++){
    t *= i;
    printf("%ld\n",t);
    p+=t;

  }
 printf("%ld\n",p); 
  return 0;
}

by JustinXiaoJunyang @ 2022-08-16 14:49:24

@123J 请用高精度


by 御坂10029号 @ 2022-08-16 14:49:34

这道题要用高精度的,long long会爆


by JustinXiaoJunyang @ 2022-08-16 14:51:28

@123J 求关注~

#include <iostream>
using namespace std;

int mul[1005], sum[1005];
void i2BIG(int x, int a[])
{
    int t = x;
    int temp = 0;
    while (t != 0)
    {
        temp++;
        a[temp] = t % 10;
        t /= 10;
    }
    a[0] = temp;
}
void mulBIG(int x[], int y, int z[])
{
    z[0] = x[0];
    int u = 0;
    for (int i = 1; i <= z[0]; i++)
    {
        int t = x[i] * y + u;
        z[i] = t % 10;
        u = t / 10;
    }
    while (u > 0)
    {
        z[0]++;
        z[z[0]] = u % 10;
        u /= 10;
    }
}
void addBIG(int x[], int y[], int z[])
{
    z[0] = max(x[0], y[0]);
    for (int i = 1; i <= z[0]; i++)
         z[i] = x[i] + y[i];
    for (int i = 1; i <= z[0]; i++)
    {
          z[i + 1] += z[i] / 10;
          z[i] %= 10;
    }
    while (z[z[0] + 1] > 0) z[0]++;
}
void printBIG(int a[]) 
{
    int la = a[0];
    for (int i = la; i >= 1; i--)
    {
        cout << a[i];
    }
    cout << endl;
}

int main()
{
    int n;
    cin >> n;
    i2BIG(0, sum);
    for (int i = 1; i <= n; i++)
    {
        i2BIG(1, mul);
        for (int j = 1; j <= i; j++)
        {
            mulBIG(mul, j, mul);
        }
        addBIG(sum, mul, sum);
    }
    printBIG(sum);
    return 0;
}

by 御坂10029号 @ 2022-08-16 14:54:54


by hyc12528 @ 2022-08-30 22:41:41

想和各位说说,人家的好像会全部WA


|