如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易。”现在,给定哈利应付的价钱 P 和他实付的钱 A,你的任务是写一个程序来计算他应该被找的零钱。
输入格式:
输入在 1 行中分别给出 P 和 A,格式为
Galleon.Sickle.Knut
,其间用 1 个空格分隔。这里Galleon
是 [0, 10?7??] 区间内的整数,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
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <sstream> 5 #include <cmath> 6 #include <algorithm> 7 #include <string> 8 #include <stack> 9 #include <queue> 10 #include <vector> 11 #include <map> 12 using namespace std; 13 14 int main() 15 { 16 long int g1, g2, s1, s2, k1, k2; 17 scanf("%ld.%ld.%ld %ld.%ld.%ld", &g1, &s1, &k1, &g2, &s2, &k2); 18 k1 = g1 * 17 * 29 + s1 * 29 + k1; 19 k2 = g2 * 17 * 29 + s2 * 29 + k2; 20 long int sub; 21 sub = k2 - k1; 22 if(sub < 0) 23 { 24 sub = -sub; 25 printf("-"); 26 } 27 int g, s, k; 28 g = sub / (17*29); 29 s = sub % (17*29) / 29; 30 k = sub % (17*29) % 29; 31 printf("%d.%d.%d", g, s, k); 32 return 0; 33 }
看到这题我首先想到的居然不是换算成最小单位,而是暴力枚举,结果是测试点2、4不过。后来看了他人的思路,突然想到这种存在单位大小换算的问题的方法中——不敢说是最简单的,但是较为方便的方法绝对是统一换算成最小单位,最后再进行单位还原的。
原文:https://www.cnblogs.com/Anber82/p/11294348.html