题目描述:
如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易。”现在,给定哈利应付的价钱 P 和他实付的钱 A,你的任务是写一个程序来计算他应该被找的零钱。
输入格式:
输入在 1 行中分别给出 P 和 A,格式为 Galleon.Sickle.Knut,其间用 1 个空格分隔。这里 Galleon 是 [ 0 , 1 0 7 ] [0, 10^7] [0,107] 区间内的整数,Sickle 是 [ 0 , 17 ) [0, 17) [0,17) 区间内的整数,Knut 是 [ 0 , 29 ) [0, 29) [0,29) 区间内的整数。
输出格式:
在一行中用与输入同样的格式输出哈利应该被找的零钱。如果他没带够钱,那么输出的应该是负数。
输入样例 1:
10.16.27 14.1.28
输出样例 1:
3.2.1
输入样例 2:
14.1.28 10.16.27
输出样例 2:
-3.2.1
代码示例(Java实现)
import java.util.Scanner;
/**
* @author snowflake
* @create-date 2019-07-18 22:20
*/
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String shouldPay = cin.next();
String currentPay = cin.next();
// 10.16.27 14.1.28
// 可看作为 百 十 个
String[] shouldPays = shouldPay.split("\\.");
String[] currentPays = currentPay.split("\\.");
// 应付
int shouldPayGalleon = Integer.parseInt(shouldPays[0]);
int shouldPaySickle = Integer.parseInt(shouldPays[1]);
int shouldPayKnut = Integer.parseInt(shouldPays[2]);
// 实付
int currentPayGalleon = Integer.parseInt(currentPays[0]);
int currentPaySickle = Integer.parseInt(currentPays[1]);
int currentPayKnut = Integer.parseInt(currentPays[2]);
// 转化为 最小单位
int should = shouldPayGalleon * 17 * 29 + shouldPaySickle * 29 + shouldPayKnut;
int current = currentPayGalleon * 17 * 29 + currentPaySickle * 29 + currentPayKnut;
// 找零
int remainder = current - should;
// 取出符号位
String sign = remainder < 0 ? "-" : "";
// 去除符号
remainder = Math.abs(remainder);
int remainderKnut = remainder % 29;
int remainderSickle = remainder / 29 % 17;
int remainderGalleon = remainder / 29 / 17;
System.out.println(sign + remainderGalleon + "." + remainderSickle + "." + remainderKnut);
}
}