1037 在霍格沃茨找零钱

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

这个计算规则我不懂诶......输入样例2,我自己算出来结果是-4.14.28......看了别人的想法,思路是把钱转换成最低单位再来计算。 以及,别人的代码好简单啊......我还把时间花在数据的读取上,其实没必要,看来对cin>>的理解还不够深入啊。

参考代码:

#include<string>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
	string str1, str2;
	cin >> str1 >> str2;
	int m = str1.size();
	int n = str2.size();
	int k = 0, f = 1;
	int num1 = 0, num2 = 0;
	vector<int>a, b;
	int flag1 = 0, flag2 = 0;
	for (int i = 0; i < m; i++)
	{
		if (str1[i] == '.')
		{
			flag1++;
			for (int j = i - 1; j >= k; j--)
			{
				num1 = num1 + (str1[j] - '0')*f;
				f = f * 10;
			}
			a.push_back(num1);
			f = 1;
			num1 = 0;
			k = i + 1;
		}
		if (flag1 == 2)
		{
			for (int j = m - 1; j >= k; j--)
			{
				num1 = num1 + (str1[j] - '0')*f;
				f = f * 10;
			}
			a.push_back(num1);
			break;
		}
	}
	k = 0, f = 1;
	for (int i = 0; i < n; i++)
	{
		if (str2[i] == '.')
		{
			flag2++;
			for (int j = i - 1; j >= k; j--)
			{
				num2 = num2 + (str2[j] - '0')*f;
				f = f * 10;
			}
			b.push_back(num2);
			f = 1;
			num2 = 0;
			k = i + 1;
		}
		if (flag2 == 2)
		{
			for (int j = n - 1; j >= k; j--)
			{
				num2 = num2 + (str2[j] - '0')*f;
				f = f * 10;
			}
			b.push_back(num2);
			break;
		}
	}
	int res[3];
	if (b[0] >= a[0])
	{
		if (b[2] >= a[2])
			res[2] = b[2] - a[2];
		else
		{
			res[2] = 29 + b[2] - a[2];
			b[1]--;
		}
		if (b[1] >= a[1])
			res[1] = b[1] - a[1];
		else
		{
			res[1] = 17 + b[1] - a[1];
			b[0]--;
		}
		res[0] = b[0] - a[0];

		cout << res[0] << "." << res[1] << "." << res[2];
	}
	else
	{
		if (a[2] >= b[2])
			res[2] = a[2] - b[2];
		else
		{
			res[2] = 29 + a[2] - b[2];
			a[1]--;
		}
		if (a[1] >= b[1])
			res[1] = a[1] - b[1];
		else
		{
			res[1] = 17 + a[1] - b[1];
			a[0]--;
		}
		res[0] = a[0] - b[0];

		cout << "-" << res[0] << "." << res[1] << "." << res[2];
	}
	system("pause");
	return 0;
}

学习这两段代码: 

来源:https://blog.csdn.net/qq_17249047/article/details/45700907

#include<iostream>
#include<cstring>
#include<map>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
 
int main()
{
    int P_G, P_S, P_K;
    int A_G, A_S, A_K;
    char c;
    while(cin>>P_G>>c>>P_S>>c>>P_K)
    {
        cin>>A_G>>c>>A_S>>c>>A_K;
        int P_Sum = P_G*17*29 + P_S*29 + P_K;
        int A_Sum = A_G*17*29 + A_S*29 + A_K;
        int K = ( abs(P_Sum - A_Sum) )%29;
        int S = ( abs(P_Sum - A_Sum) )/29%17;
        int G =  ( abs(P_Sum - A_Sum) )/29/17;
        if( P_Sum<=A_Sum )
            cout<<G<<'.'<<S<<'.'<<K<<endl;
        else
            cout<<'-'<<G<<'.'<<S<<'.'<<K<<endl;
    }
    return 0;
}

来源:https://blog.csdn.net/wanmeiwushang/article/details/51460991

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
    int Galleon1,Sickle1,Knut1,sum1=0;
    int Galleon2,Sickle2,Knut2,sum2=0;
    int fee;
    scanf("%d.%d.%d",&Galleon1,&Sickle1,&Knut1);
    scanf("%d.%d.%d",&Galleon2,&Sickle2,&Knut2);
    sum1=Knut1+Sickle1*29+Galleon1*17*29;//换算成最小单位
    sum2=Knut2+Sickle2*29+Galleon2*17*29;
    Galleon1=Sickle1=Knut1=0;            //已经用过了,可以拿来存最后结果
    fee=abs(sum2-sum1);                  //取绝对值先算出差来
    while(fee>0){                        
        if(fee<29){                      //不到换算的条件break直接输出
            Knut1=fee;
            break;
        }else {                          //满29进1
            fee-=29;
            Sickle1++;
            if(Sickle1>=17){             //满17进1
                Sickle1-=17;
                Galleon1++;
            }
        }

    }   
    if(sum2-sum1>=0)
    printf("%d.%d.%d\n",Galleon1,Sickle1,Knut1);
    else printf("-%d.%d.%d\n",Galleon1,Sickle1,Knut1);
    return 0;
}

 感觉c语言的输入输出好方便,pat的格式要求就是用c来写很简单。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值