码迷,mamicode.com
首页 > 其他好文 > 详细

PAT B1037 在霍格沃兹找零钱

时间:2019-08-04 19:52:36      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:end   after   swap   scan   价格   name   else   txt   stdin   

AC代码

#include <cstdio>
#include <algorithm>

using namespace std;

char flag = 0; //判断付钱数是否大于价格
struct Money {
    int Galleon, Sickle, Knut;
} price, pay, temp, sub;

void init() {
    price.Galleon = pay.Galleon = temp.Galleon = 0;
    price.Sickle = pay.Sickle = temp.Sickle = 0;
    price.Knut = pay.Knut = temp.Knut = 0;
}
void Sub(Money a, Money b) {  //a为付钱, b为价格
    if(a.Knut >= b.Knut) {
        temp.Knut = a.Knut - b.Knut;
    } else if(a.Knut < b.Knut) {
        a.Sickle -= 1;
        temp.Knut = a.Knut + 29 - b.Knut;
    }
    //printf("a.Sickle = %d c.Knut = %d\n", a.Sickle, c.Knut);
    //printf("before_____a.Galleon = %d\n", a.Galleon);
    if(a.Sickle >= b.Sickle) {
        temp.Sickle = a.Sickle - b.Sickle;
    } else if(a.Sickle < b.Sickle) {
        a.Galleon -= 1;
        temp.Sickle = a.Sickle + 17 - b.Sickle;
    }
    //printf("after_____a.Galleon = %d\n", a.Galleon);
    temp.Galleon = a.Galleon - b.Galleon;
}

int main() {
    #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    init();
    scanf("%d.%d.%d %d.%d.%d", &price.Galleon, &price.Sickle, &price.Knut, &pay.Galleon, &pay.Sickle, &pay.Knut);
//    printf("price:%d.%d.%d  pay:%d.%d.%d\n", price.Galleon, price.Sickle, price.Knut, pay.Galleon, pay.Sickle, pay.Knut);
    if(pay.Galleon < price.Galleon) {
        swap(price, pay);
        flag = 1;
    }
//    printf("flag:%c\n", flag);
//    printf("price:%d.%d.%d  pay:%d.%d.%d\n", price.Galleon, price.Sickle, price.Knut, pay.Galleon, pay.Sickle, pay.Knut);
    Sub(pay, price);
    if(flag == 1) printf("-");
    printf("%d.%d.%d", temp.Galleon, temp.Sickle, temp.Knut);
    return 0;
}

PAT B1037 在霍格沃兹找零钱

标签:end   after   swap   scan   价格   name   else   txt   stdin   

原文地址:https://www.cnblogs.com/isChenJY/p/11299053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!