PAT B1037 在霍格沃兹找零钱
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;
}
吾生也有涯,而知也无涯。