1037 在霍格沃茨找零钱 (20 分)
如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易。”现在,给定哈利应付的价钱 P 和他实付的钱 A,你的任务是写一个程序来计算他应该被找的零钱。
输入在 1 行中分别给出 P 和 A,格式为 Galleon.Sickle.Knut
,其间用 1 个空格分隔。这里 Galleon
是 [0, 107] 区间内的整数,Sickle
是 [0, 17) 区间内的整数,Knut
是 [0, 29) 区间内的整数。
在一行中用与输入同样的格式输出哈利应该被找的零钱。如果他没带够钱,那么输出的应该是负数。
10.16.27 14.1.28
3.2.1
14.1.28 10.16.27
-3.2.1
【我的代码】
// 1037 在霍格沃茨找零钱 (20 分).cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <cstdio>
using namespace std;
int changeMoney(int S, int K, int G) {
int P_All_S, P_ALL_K;
P_All_S = S + G * 17;
P_ALL_K = P_All_S * 29 + K;
return P_ALL_K;
}
int main()
{
//输入
int P_S, P_G, P_K;
int A_S, A_G, A_K;
scanf_s("%d.%d.%d", &P_G, &P_S, &P_K);
scanf_s("%d.%d.%d", &A_G, &A_S, &A_K);
//先全部换成最小的单位
int All_P_K, All_A_K;
All_P_K = changeMoney(P_S, P_K, P_G);
All_A_K = changeMoney(A_S, A_K, A_G);
//作差判断正负
int cha = All_A_K - All_P_K;
if (!cha)
cout << "0.0.0";
else if (cha > 0) {
int a = cha % 29;
cha /= 29;
int b = cha % 17;
int c = cha / 17;
printf("%d.%d.%d", c, b, a);
}
else {
cha *= -1;
int a = cha % 29;
cha = cha / 29;
int b = cha % 17;
int c = cha / 17;
printf("-%d.%d.%d", c, b, a);
}
return 0;
}
【总结】