# B1037. 在霍格沃茨找零钱（20）

10.16.27 14.1.28


3.2.1


14.1.28 10.16.27


-3.2.1

#include <cstdio>

struct money {
int gelleon;
int sickle;
int knut;
}p, a;

int main()
{
scanf ("%d.%d.%d", &p.gelleon, &p.sickle, &p.knut);
scanf ("%d.%d.%d", &a.gelleon, &a.sickle, &a.knut);
int price1, price2;
price1 = p.gelleon * 29 * 17 + p.sickle * 29 + p.knut;            //转化为Knut进行计算；
price2 = a.gelleon * 29 * 17 + a.sickle * 29 + a.knut;

int ans = price2 - price1;
if (ans < 0) {					//判断是否需要输出负号，和对结果进行修正；
printf ("-");
ans = -ans;
}

printf ("%d.%d.%d\n", ans / (29 * 17), ans % (29 * 17) / 29, ans % 29);

return 0;
} 

P和A的加减从低位开始，不够减时向前一位借位（需考虑前一位为0的情况）。

#include <cstdio>

struct money {
int gelleon;
int sickle;
int knut;
}p, a, change;

bool more(money a, money b) {				//判断P和A的大小；
if (a.gelleon != b.gelleon)		return a.gelleon > b.gelleon;
if (a.sickle != b.sickle)  		return a.sickle > b.sickle;
return  	a.knut > b.knut;
}

void swap (money *a, money *b) {			//用于交换P、A；
struct money temp;
temp = *a;
*a = *b;
*b = temp;
}

int main()
{
scanf ("%d.%d.%d", &p.gelleon, &p.sickle, &p.knut);
scanf ("%d.%d.%d", &a.gelleon, &a.sickle, &a.knut);

if (more (p, a) != 0) {
swap (&p, &a);
printf ("-");
}
if (a.knut >= p.knut)		change.knut = a.knut - p.knut;
else {
change.knut = a.knut + 29 - p.knut;
if (a.sickle !=0)       a.sickle--;				//向sickle借位；
else {								//sickle为0时，再向gelleon借位；
a.sickle = a.sickle + 17 - 1;
a.gelleon--;
}
}

if (a.sickle >= p.sickle)		change.sickle = a.sickle - p.sickle;
else {
change.sickle = a.sickle + 17 - p.sickle;
a.gelleon--;
}

change.gelleon = a.gelleon - p.gelleon;

printf ("%d.%d.%d\n", change.gelleon, change.sickle, change.knut);

return 0;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120