天天看點

PAT 1037 1037. 在霍格沃茨找零錢(20)

1037. 在霍格沃茨找零錢(20)

時間限制 400 ms

記憶體限制 65536 kB

代碼長度限制 8000 B

判題程式 Standard 作者 CHEN, Yue

如果你是哈利·波特迷,你會知道魔法世界有它自己的貨币系統 —— 就如海格告訴哈利的:“十七個銀西可(Sickle)兌一個加隆(Galleon),二十九個納特(Knut)兌一個西可,很容易。”現在,給定哈利應付的價錢P和他實付的錢A,你的任務是寫一個程式來計算他應該被找的零錢。

輸入格式:

輸入在1行中分别給出P和A,格式為“Galleon.Sickle.Knut”,其間用1個空格分隔。這裡Galleon是[0, 107]區間内的整數,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
      
#include <iostream>
#include <stdio.h>
using namespace std;

class Money
{
public:
	int G,S,K;
	bool operator>(const Money & b);
};

bool Money::operator>(const Money & b)
{
	if( this->G > b.G )
		return true;
	else if( (this->G==b.G) && (this->S>b.S) )
		return true;
	else if( (this->G==b.G) && (this->S==b.S) && (this->K>b.K) )
		return true;
	else
		return false;
}

Money P,A,Charge,temp;
bool flag;

int main()
{
	scanf("%d.%d.%d %d.%d.%d",&P.G,&P.S,&P.K,&A.G,&A.S,&A.K);

	if( P>A )
	{
		temp = P;
		P = A;
		A = temp;
		cout<<"-";
	}


	if( A.K - P.K>=0 )
		Charge.K = A.K - P.K ;
	else
	{
		Charge.K = A.K - P.K + 29;
		A.S--;
	}

	if( A.S - P.S>=0 )
		Charge.S = A.S - P.S ;
	else
	{
		Charge.S = A.S - P.S + 17;
		A.G--;
	}

	Charge.G = A.G - P.G;

	cout<<Charge.G<<"."<<Charge.S<<"."<<Charge.K<<endl;

	return 0;
}
           
PAT 1037 1037. 在霍格沃茨找零錢(20)

繼續閱讀