求助!全WA!c++

P1591 阶乘数码

yc123bc @ 2024-01-31 18:56:22

我卡在求n^2了! 代码献上:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<string>
using namespace std;
const int N=3500;
int n;
struct bigint{  //???
    string num;
    friend bigint operator *(const bigint a,const bigint b){
        int yi[N],er[N],ji[N];
        memset(yi,0,sizeof(yi));
        memset(er,0,sizeof(er));
        memset(ji,0,sizeof(ji));
        int len1=a.num.size();
        int len2=b.num.size();
        for(int i=0;i<len1;i++){
            yi[i]=a.num[len1-i-1]-48;
        }
        for(int i=0;i<len2;i++){
            er[i]=b.num[len2-i-1]-48;
        }
        for(int i=0;i<len1;i++){
            for(int j=0;j<len2;j++){
                ji[i+j]=yi[i]*er[j];
            }
        }
        int len=len1+len2;
        for(int i=0;i<len;i++){
            if(ji[i]>=10){
                ji[i+1]+=ji[i]/10;
                ji[i]%=10;
            }
        }
        bigint ans;
        ans.num="";
        for(int i=len-1;i>=0;i--){
            ans.num=ans.num+char(ji[i]+48);
        }
        return ans;
    }
};
int main()
{
    bigint t1;
    cin>>n;
    for(int i=1;i<=n;i++){
        char t2;
        int sum=0;
        cin>>t1.num>>t2;
        bigint t3;
        t3.num=""+t2;
        bigint p;
        p.num=(t1*t3).num;
        int len=p.num.size();
        for(int j=0;j<len;j++){
            if(p.num[j]==t2){
                sum++;
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

求一位大佬


by ikun_god @ 2024-01-31 19:15:06

这边建议手打高精

我的代码(别抄)

#include <iostream>
#include <cstring>
using namespace std;
const int MAX_LEN = 50000;
struct BigInt
{
    int v[MAX_LEN];
    int len;
    int sign;
    BigInt(int n) {
        memset(v,0,sizeof(v));
        if (n>=0){
            sign=1;
        }
        else{
            sign=-1;
        }
        int i=0;
        while (true){
            v[i++]=n%10;
            n/=10;
            if (n==0){
                break;
            }
        }
        len=i;
    }
    BigInt(const char* a) {
        len=strlen(a);
        if (a[0]=='-'){
            sign=-1;
            len-=1;
            for (int i=0;i<=len;++i){
                if (a[len-i]!='-'){
                    v[i]=a[len-i]-'0';
                }
            }
        }
        else{
            sign=1;
            for (int i=0;i<len;++i){
                v[len-i-1]=a[i]-'0';
            }
        }

    }
    int compare(const BigInt& b) {
        if (len<b.len){
            return -1;
        }
        if (len>b.len){
            return 1;
        }
        for (int i=len-1;i>=0;--i){
            if (v[i]<b.v[i]){
                return -1;
            }
            if (v[i]>b.v[i]){
                return 1;
            }
        }
        return 0;
    }
    void add(const BigInt& b) {
        int i=0,x=0;
        int blen=b.len;
        while ((i<len) || (i<blen)){
            v[i]=v[i]+b.v[i]+x;
            x=v[i]/10;
            v[i]%=10;
            ++i;
        }
        if (x > 0){
            v[i]=x;
            len = i + 1;
        } else {
            len = i;
        }
    }
    void sub(const BigInt& b) {
        int i=0,x=0,y[MAX_LEN];
        int blen=b.len;
        int f=compare(b);
        if (f==0){
            len=1;
            v[0]=0;
            return;
        }
        if (f>=1){
            for (i=0;i<len;++i){
                if (v[i]<b.v[i]){
                    v[i]+=10;
                    v[i+1]-=1;
                }
                v[i]-=b.v[i];
            }
            for (int i=len-1;i>=0;--i){
                if (v[i]==0){
                    len-=1;
                }
                else{
                    break;
                }
            }
        }
        else{
            for (i=0;i<len;++i){
                if (b.v[i]<v[i]){
                    //b.v[i]+=10;
                    v[i+1]-1;
                }
                v[i]-=b.v[i];
            }
            for (int i=0;i<len;++i){
                if (v[i]==0){
                    len-=1;
                }

            }
        }
    }
    void mul(const BigInt& b) {
        int c[MAX_LEN];
        memset(c,0,sizeof(c));
        for (int i=0;i<len;++i){
            int x=0;
            for (int j=0;j<b.len;++j){
                c[i+j]=v[i]*b.v[j]+x+c[i+j];
                x=c[i+j]/10;
                c[i+j]=c[i+j]%10;
            }
            c[i+b.len]=x;
        }
        memcpy(v,c,sizeof(c));
        len = len + b.len;
        while(v[len-1]==0){
            len--;
        }
    }
    void mul(int b) 
    {
        int x=0;
        for (int i=0;i<len;++i){
            v[i]=v[i]*b+x;
            x=v[i]/10;
            v[i]=v[i]%10;
        }
        while (x) {
            v[len++]=x%10;
            x/=10;
        }
    }

    void div(int b,int& m) {
        int x=0;
        for (int i=len-1;i>=0;--i){
            int t=v[i];
            v[i]=(x*10+v[i])/b;
            x=(x*10+t)%b;
        }
        m = x;
        for (int i=len-1;i>=0;--i){
            if (v[i]==0){
                len-=1;
            }
            else{
                break;
            }
        }
    }
    void div(int b) {
        long long x=0;
        for (int i=len-1;i>=0;--i){
            int t=v[i];
            v[i]=(x*10+v[i])/b;
            x=(x*10+t)%b;
            //cout << x << endl;
        }
        for (int i=len-1;i>=1;--i){
            if (v[i]==0){
                len-=1;
            }
            else{
                break;
            }
        }
    }
    void div(const BigInt&b) {
        int x=0;
        for (int i=len-1;i>=0;--i){
            for (int j=0;j<b.len;++j){
                int t=v[i];
                v[i]=(x*10+v[i])/b.v[j];
                x=(x*10+t)%b.v[j];
            }
        }

        for (int i=len-1;i>=0;--i){
            if (v[i]==0){
                len-=1;
            }
            else{
                break;
            }
        }
    }
};
std::ostream& operator<<(std::ostream& os, const BigInt& b)
{
    if (b.sign<0){
        cout<<"-";
    }
    for(int i = b.len - 1;i >= 0; --i){
        os << b.v[i];
    }
    return os;
}
int main()
{
    int t;
    cin>>t;
    while (t--){
        int n,v,sum=0;
        cin>>n>>v;
        BigInt a(1);
        for (int j=2;j<=n;++j){
            a.mul(j);
        }
        for (int j=0;j<a.len;++j){
            if (a.v[j]==v){
                sum+=1;
            }
        }
        cout<<sum<<endl;
    }
}

by ikun_god @ 2024-01-31 19:15:20

@yc123bc


by yc123bc @ 2024-01-31 19:16:39

@ikun_god 好长啊!


by yc123bc @ 2024-01-31 19:18:34

@ikun_god 除了部分,其他都看不懂


by ikun_god @ 2024-01-31 19:20:15

其实核心部份只有mul,其余是我自己备用的


by yc123bc @ 2024-01-31 19:21:25

有没有其他大佬?我想多看几种解题方式。


by C_plus_plus_12345 @ 2024-02-01 16:51:04

@ikun_god 刷新了我对结构体的认知


by qqq123456qqq @ 2024-04-06 17:37:35

@ikun_god 666


|