PTA 1037 在霍格沃茨找零钱 (20 分) C++实现

1037 在霍格沃茨找零钱 (20 分)

如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(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

解题思路:

全部转换成最小单位k,再进行进制转换

代码示例:

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
using namespace std;

int main()
{
	string price, money;
	cin >> price >> money;
	
	string num_p = "";
	string num_m = "";

	//利用数组取得price与money的g,s,k
	vector<int> pri;
	for (int i = 0; i < price.length(); i++)
	{
		if (price[i] != '.')
		{
			num_p += price[i];
		}
		else
		{
			pri.push_back(stoi(num_p));
			num_p = "";
		}
	}
	pri.push_back(stoi(num_p));

	vector<int> mon;
	for (int i = 0; i < money.length(); i++)
	{
		if (money[i] != '.')
		{
			num_m += money[i];
		}
		else
		{
			mon.push_back(stoi(num_m));
			num_m = "";
		}
	}
	mon.push_back(stoi(num_m));

	int p_k = pri[0] * 493 + pri[1] * 29 + pri[2];
	int m_k = mon[0] * 493 + mon[1] * 29 + mon[2];

	int total = abs(m_k - p_k);

	int g = total / 493;
	total -= g * 493;

	int s = total / 29;
	total -= s * 29;

	int k = total;

	if (m_k < p_k)
	{
		cout << "-";
	}
	cout << g << "." << s << "." << k << endl;
}

运行结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Crazy.宥思

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值