PAT BASIC LEVEL 1037. 在霍格沃茨找零钱(20)

1037. 在霍格沃茨找零钱(20)

如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易。”现在,给定哈利应付的价钱P和他实付的钱A,你的任务是写一个程序来计算他应该被找的零钱。

输入格式:

输入在1行中分别给出P和A,格式为“Galleon.Sickle.Knut”,其间用1个空格分隔。这里Galleon是[0, 107]区间内的整数,Sickle是[0, 17)区间内的整数,Knut是[0, 29)区间内的整数。

输出格式:
在一行中用与输入同样的格式输出哈利应该被找的零钱。如果他没带够钱,那么输出的应该是负数。

输入样例1:
10.16.27 14.1.28

输出样例1:
3.2.1

输入样例2:
14.1.28 10.16.27

输出样例2:
-3.2.1

Answer:

#include<iostream>
using namespace std;
struct money {
    int gal;
    int sic;
    int knu;
    inline money() {
        this->gal = 0;
        this->sic = 0;
        this->knu = 0;
    }
    inline void exp() {
        cout << gal << '.' << sic << '.' << knu;
    }
    inline void diff(money* a) {
        if(this->greater_equal(a)) {
            this->gal -= a->gal;
            this->sic -= a->sic;
            this->knu -= a->knu;
            if(this->knu < 0) {
                this->sic--;
                this->knu += 29;
            }
            if(this->sic < 0) {
                this->gal--;
                this->sic += 17;
            }
        } else {
            this->gal = a->gal - this->gal;
            this->sic = a->sic - this->sic;
            this->knu = a->knu - this->knu;
            if(this->knu < 0) {
                this->sic--;
                this->knu += 29;
            }
            if(this->sic < 0) {
                this->gal--;
                this->sic += 17;
            }
            this->gal *= -1;
        }
    }
    inline bool greater_equal(money *a) {
        if(this->gal > a->gal)
            return true;
        else if(this->gal < a->gal)
            return false;
        else if(this->sic > a->sic)
            return true;
        else if(this->sic < a->sic)
            return false;
        else if(this->knu > a->knu)
            return true;
        else if(this->knu < a->knu)
            return false;
        else
            return true;
    }
};
int main() {
    char pay[15];
    char act[15];
    money *p = new money();
    money *a = new money();
    cin >> pay >> act;
    int i = 0;
    while(pay[i] != '.')
        p->gal = p->gal*10 + pay[i++] - '0';
    i++;
    while(pay[i] != '.')
        p->sic = p->sic*10 + pay[i++] - '0';
    i++;
    while(pay[i])
        p->knu = p->knu*10 + pay[i++] - '0';
    i = 0;
    while(act[i] != '.')
        a->gal = a->gal*10 + act[i++] - '0';
    i++;
    while(act[i] != '.')
        a->sic = a->sic*10 + act[i++] - '0';
    i++;
    while(act[i])
        a->knu = a->knu*10 + act[i++] - '0';
    a->diff(p);
    a->exp();
}

PS.
又一次通过。
唉,肤浅如我啊。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值