C++ 求大佬解答 wa了最后一个

P1009 [NOIP1998 普及组] 阶乘之和

Liquidice @ 2022-09-23 14:46:14

#include <iostream>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;

struct hugeint
{
    int arr[10004] = {0};
    int len = 1;
};

hugeint fec(long long, hugeint);

hugeint jia(hugeint a, hugeint b)
{
    hugeint ans;
    ans.len = max(a.len, b.len);
    for (int i = 1; i <= ans.len; i++)
    {
        ans.arr[i] += a.arr[i] + b.arr[i];
        ans.arr[i + 1] += ans.arr[i] / 10;
        ans.arr[i] %= 10;
    }
    if (ans.arr[ans.len + 1] > 0)
    {
        ans.len += 1;
    }
    return ans;
}
hugeint cheng(hugeint a, hugeint b)
{
    hugeint ans;
    ans.len = a.len + b.len - 1;
    for (int j = 1; j <= a.len; j++)
    {
        for (int i = 1; i <= b.len; i++)
        {
            ans.arr[j + i - 1] += a.arr[j] * b.arr[i];
            ans.arr[j + i] += ans.arr[j + i - 1] / 10;
            ans.arr[j + i - 1] %= 10;
        }
    }
    if (ans.arr[ans.len + 1] > 0)
    {
        ans.len += 1;
    }
    return ans;
}

void print(hugeint a)
{
    for (int i = a.len; i > 0; i--)
    {
        cout << a.arr[i];
    }
    cout << endl;
}
int main()
{
    long long n = 1;
    hugeint S;
    hugeint t;
    hugeint temp;
    hugeint null;
    int x = 1;
    cin >> n;
    null.arr[1] = 1;
    for (int i = 0; i < n;) // jia
    {
        temp = null;
        for (int i = n; i > 0; i--)
        {
            t.arr[1] = i;
            temp = cheng(temp, t);
        }
        S = jia(S, temp);
        n = n - 1;
    }

    print(S);
    system("pause");
}

by MarchKid_Joe @ 2022-09-23 14:52:58

@Liquidice 我用 cmd 比较你的 Ac 代码和这个讨论的代码, cmd 告诉我比较无差异。所以这代码没错吧。。。。。。


by MarchKid_Joe @ 2022-09-23 14:56:45

感觉我被骗了


by Liquidice @ 2022-09-23 15:02:26

@MarchKid_Joe 输入50结果为:1002809516549005962847556730632934272920420940313 正确的50结果应该是31035053229546199656252032972759319953190362094566672920420940313 不知道为啥,前几十都是对的,近50的数都错了,然后最后一个测试点也wa了


by MarchKid_Joe @ 2022-09-23 15:43:24

@Liquidice bug de 完了,似乎确实有很多 bug.


by MarchKid_Joe @ 2022-09-23 15:44:10

没有大改你的代码,错误标出来了


by MarchKid_Joe @ 2022-09-23 15:44:59

@Liquidice

#include <iostream>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;

struct hugeint
{
    int arr[10004] = {0};
    int len = 1;
    void number(int num)
    {
        //比如 12 ,
        //你不能只把 12 放到 arr[1] 内 。
        //应该把 2 放到 arr[1] 。 1 放到 arr[2]。
        //倒序存储。
        len =0;
        while(num > 0)
        {
            arr[++len] = num % 10;
            num /= 10;
        }
    }
};

hugeint fec(long long, hugeint);

hugeint jia(hugeint a, hugeint b)
{
    hugeint ans;
    ans.len = max(a.len, b.len);
    for (int i = 1; i <= ans.len; i++)
    {
        ans.arr[i] += a.arr[i] + b.arr[i];
        ans.arr[i + 1] += ans.arr[i] / 10;
        ans.arr[i] %= 10;
    }
    if (ans.arr[ans.len + 1] > 0)
    {
        ans.len += 1;
    }
    return ans;
}
hugeint cheng(hugeint a, hugeint b)
{
    hugeint ans;
    ans.len = a.len + b.len;//不能减1  3*4=12 两位数
    for (int j = 1; j <= a.len; j++)
    {
        for (int i = 1; i <= b.len; i++)
        {
            ans.arr[j + i - 1] += a.arr[j] * b.arr[i];
            ans.arr[j + i] += ans.arr[j + i - 1] / 10;
            ans.arr[j + i - 1] %= 10;
        }
    }
    //因为是乘法,这样去除前导零。
    while(ans.len > 1 && ans.arr[ans.len] == 0) ans.len--;
    // if (ans.arr[ans.len + 1] > 0)
    // {
    //     ans.len += 1;
    // }
    return ans;
}

void print(hugeint a)
{
    for (int i = a.len; i > 0; i--)
    {
        cout << a.arr[i];
    }
    cout << endl;
}
int main()
{
    long long n = 1;
    hugeint S;
    hugeint t;
    hugeint temp;
    hugeint null;
    int x = 1;
    cin >> n;
    null.arr[1] = 1;
    for (int i = 0; i < n;) // jia
    {
        temp = null;
        for (int i = n; i > 0; i--)
        {
            t.number(i);
            // t.arr[1] = i; 错误的
            temp = cheng(temp, t);
        }
        S = jia(S, temp);
        n = n - 1;
    }

    print(S);
    system("pause");
}

by Liquidice @ 2022-09-23 23:29:14

@MarchKid_Joe 感谢大佬,辛苦了


by ResonCaolol @ 2022-09-30 16:01:58

@Liquidice

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=10000;
int main()
{
    int n,k,i,j,t;
    while(cin>>n){
        int a[maxn]={},sum[maxn]={};
        sum[0]=0;
        k=1,t=1;
        for(int e=1;e<=n;e++){
            a[0]=1;
            for(i=1;i<=e;i++){
                for(j=0;j<k;j++){    
                    a[j]=a[j]*i;        
                }
                for(j=0;j<k;j++){
                    if(a[j]>=10){
                        a[j+1]+=a[j]/10;
                        a[j]=a[j]%10;
                        if(j==k-1) k++; 

                    }       
                }
            }
            for(int p=0;p<k;p++){
                sum[p]=sum[p]+a[p];
            }   
            for(int p=0;p<t;p++){
                if(sum[p+1]>=10){
                    t++;
                }
                if(sum[p]>=10){
                    sum[p+1]+=sum[p]/10;
                    sum[p]=sum[p]%10;
                }       
            }
            for(int q=1;q<k;q++){
                a[q]=0;
            }
        }
        for(i=k-1;i>=0;i--){
            cout<<sum[i]<<"";
        }
        cout<<endl;
    }
return 0;
}

|