薛定谔的C++
  • 板块灌水区
  • 楼主LTTXiaochuan
  • 当前回复2
  • 已保存回复2
  • 发布时间2025/1/27 10:06
  • 上次更新2025/1/27 15:01:44
查看原帖
薛定谔的C++
1391256
LTTXiaochuan楼主2025/1/27 10:06

那天,我正准备把国王游戏这道题补一下,顺手写个高精模板方便下次用,结果发生了不好的事情......

第130行,我将tmp输出,结果导致了下面另一个输出的结果发生了变化???就是说,如果我输出tmp,会导致另一个输出被影响???

望大佬答疑解惑!

#include <bits/stdc++.h>
using namespace std;

const int l=1000;

struct HP{
    int d[2*l+10];
    HP(){ for(int i=0; i<2*l+10; i++) d[i]=0; };
    HP(long long x)
    {
        for(int i=0; i<2*l+10; i++) d[i]=0;
        int cnt=0;
        while(x) d[++cnt]=x%10,x/=10;
    }
    HP(string str)
    {
        for(int i=0; i<2*l+10; i++) d[i]=0;
        int cnt=0;
        for(int i=str.size()-1; i>=0; i--) d[++cnt]=str[i]-'0';
    }
    void print(void)
    {
        bool flag=0;
        for(int i=l; i>=1; i--)
        {
            if(d[i]) flag=1;
            if(flag) cout<<d[i];
        }
        if(!flag) cout<<0;
    }
    HP operator=(const HP &b)
    {
        HP a=*this;
        for(int i=1; i<=l; i++) a.d[i]=b.d[i];
        return a;
    }
    HP operator+(const HP &b)
    {
        int jw=0;
        HP a=*this,c;
        for(int i=1; i<=l; i++)
        {
            c.d[i]=(a.d[i]+b.d[i]+jw)%10;
            jw=(a.d[i]+b.d[i]+jw)/10;
        }
        return c;
    }
    HP operator-(const HP &b)
    {
        int tw=0;
        HP a=*this,c;
        for(int i=1; i<=l; i++)
        {
            c.d[i]=a.d[i]-tw-b.d[i];
            tw=0;
            if(c.d[i]<0) c.d[i]+=10,tw=1;
        }
        return c;
    }
    HP operator*(const HP &b)
    {
        HP a=*this,c;
        for(int i=1; i<=l; i++)
            for(int j=1; j<=l; j++)
            {
                int pos=i+j-1;
                c.d[pos]+=a.d[i]*b.d[j];
                if(c.d[pos]>9) c.d[pos+1]+=c.d[pos]/10,c.d[pos]%=10;
            }
        return c;
    }
    HP operator/(long long b)
    {
        if(b==0){ cerr<<"ERROR!\n"; return 0; }
        string str;
        HP a=*this;
        int tmp=0,flag=0;
        for(int i=l; i>=1; i--)
        {
            if(!a.d[i] && !flag) continue;
            flag=1;
            tmp=tmp*10+a.d[i];
            str=str+char(tmp/b+'0');
            if(tmp/b) tmp%=b;
        }
        return HP(str);
    }
    bool operator<(const HP &b)const
    {
        HP a=*this;
        for(int i=l; i>=1; i--)
        {
            if(a.d[i]<b.d[i]) return true;
            if(a.d[i]>b.d[i]) return false;
        }
        return false;
    }
    bool operator>(const HP &b)const
    {
        if(*this<b) return false;
        else return true;
    }
};

const int N=1010;

struct DC{
    int l,r;
}dc[N];

bool cmp(DC a,DC b){ return a.l*1./a.r<b.l*1./b.r; }

int main()
{
    //ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    int n;
    cin>>n>>dc[0].l>>dc[0].r;
    for(int i=1; i<=n; i++) cin>>dc[i].l>>dc[i].r;
    sort(dc+1,dc+1+n,cmp);
    HP ans,tmp=HP(1);
    for(int i=1; i<=n; i++)
    {
        tmp=HP(dc[i-1].l)*tmp;
        tmp.print();cout<<"\n";
        (HP(dc[i-1].l)*tmp).print();cout<<"\n";
        HP shabi=tmp/dc[i].r;
        if(shabi>ans) ans=shabi;
    }

    ans.print();

    return 0;
}
2025/1/27 10:06
加载中...