PAT 乙级 1037 在霍格沃茨找零钱

本题的核心是实现29进制和17进制的减法用程序手动模拟借位减法就可以了

#include <iostream>

using namespace std;

struct Money
{
    long x,y,z;
    bool isNegative;
    Money()
    {
        isNegative=false;
    }
};

ostream& operator<<(ostream& str, const Money& m);
istream& operator>>(istream& str,Money& m);
Money operator-(const Money& a,const Money& b);
bool operator>(const Money& a,const Money& b);
bool operator==(const Money& a,const Money& b);
bool operator>=(const Money& a,const Money& b);

int main()
{
    Money a,b;
    cin>>a>>b;
    cout<<b-a;
    return 0;
}

ostream& operator<<(ostream& str, const Money& m)
{
    if(m.isNegative)
        str<<'-';
    str<<m.x<<'.'<<m.y<<'.'<<m.z;
    return str;
}

istream& operator>>(istream& str,Money& m)
{
    cin>>m.x;
    getchar();
    cin>>m.y;
    getchar();
    cin>>m.z;
    return str;
}

bool operator>(const Money& a,const Money& b)
{
    if(a.x>b.x)
        return true;
    else
    {
        if(a.x<b.x)
            return false;
        else
        {
            if(a.y>b.y)
                return true;
            else
            {
                if(a.y<b.y)
                    return false;
                else
                {
                    if(a.z>b.z)
                        return true;
                    else
                    {
                        return false;
                    }
                }
            }
        }
    }
}

bool operator==(const Money& a,const Money& b)
{
    return a.x==b.x && a.y==b.y && a.z==b.z;
}

bool operator>=(const Money& a,const Money& b)
{
    return  a>b || a==b;
}

Money operator-(const Money& a,const Money& b)
{
    Money c;
    if(a>=b)
    {
        c=a;
        if(c.z<b.z)
        {
            c.y-=1;
            c.z=29-b.z+c.z;
        }
        else
            c.z-=b.z;
        if(c.y<b.y)
        {
            c.x-=1;
            c.y=17-b.y+c.y;
        }
        else
            c.y-=b.y;
        c.x-=b.x;
    }
    else
    {
        c=b;
        c.isNegative=true;
        if(c.z<a.z)
        {
            c.y-=1;
            c.z=29-a.z+c.z;
        }
        else
            c.z-=a.z;
        if(c.y<a.y)
        {
            c.x-=1;
            c.y=17-a.y+c.y;
        }
        else
            c.y-=a.y;
        c.x-=a.x;
    }
    return c;
}

我会一直在,我会一直走!

猜你喜欢

转载自www.cnblogs.com/FDProcess/p/9231411.html