1037. Find change at Hogwarts (20) time limit MS Memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Chen, Yue
If you are a Harry Potter fan, you will know that the Wizarding world has its own monetary system-as Hagrid told Harry: "17 Silver Sickle (Galleon), 29 Nat (Knut) against a west, is easy." "Now, given the price Harry has to deal with and the money he pays a, your task is to write a program to calculate the change he should be looking for."
Input format:
The input gives p and a in 1 rows, in the form "Galleon.Sickle.Knut", separated by 1 spaces. Here galleon is an integer in the [0, 107] interval, sickle is an integer within the interval of [0, 17), and Knut is an integer within the [0, 29] interval.
Output format:
Output the change Harry should be looking for in a row with the same format as the input. If he does not have enough money, then the output should be negative.
Input Sample 1:
10.16.27 14.1.28
Output Example 1:
3.2.1
Input Sample 2:
14.1.28 10.16.27
Output Example 2:
-3.2.1
Difficulty: How to input three integers in A.B.C form to read successfully
Train of thought 2 difficulty: the output place, must satisfy the fixed output way
Train of thought 1: First convert P and a of 2 money to the minimum measure number, then the difference, and then converted to the form of the three-level unit representation
Train of thought 2: self-connected plus minus complement
Idea 1 Code:
1 //1037.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6 7 using namespacestd;8 9 class MoneyTen { One Public: A intGalleon; - intSickle; - intKnut; the - intget_knut (); -Money Get_money (intKnut); - voidShow_money (Money M,BOOLb); + }; - + intMain () A { at Money P, a,temp; - -CIN >>P.galleon; -GetChar ();//read. -CIN >>P.sickle; - GetChar (); inCin>>P.knut; - toCIN >>A.galleon; + GetChar (); -CIN >>A.sickle; the GetChar (); *CIN >>A.knut; $ Panax Notoginseng //multiple functions integrated together -Temp.show_money (Temp.get_money (A.get_knut ()-P.get_knut ()), (A.get_knut ()-P.get_knut ()) >=0?1:0); the } + A //Calculate total Knut the intMoney::get_knut () + { - return -* in* This->galleon + in* This->sickle + This-Knut; $ } $ - //will return an object of a class that stores the spread -Money Money::get_money (intKnut) the { - This->galleon = Knut/ -/ in;WuyiKnut-= Galleon * -* in; the This->sickle = knut/ in; -Knut-= Sickle * in; Wu This->knut =Knut; - About return* This; $ } - - //output results as a class - voidMoney::show_money (Money M,BOOLb) A { + if(b) thecout << M.galleon <<"."<< M.sickle <<"."<< M.knut <<Endl; - Else $cout << M.galleon <<"."<<-m.sickle <<"."<<-m.knut <<Endl; the}
Of course, the 38th line is very poor readability, but also just a few functions together, looks more complex, in fact, the content is very simple
Idea 2 Code:
1 //1037_1.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6 7 using namespacestd;8 9 class MoneyTen { One Public: A intGalleon; - intSickle; - intKnut; the }; - - intMain () - { + Money P, A, temp; - +CIN >>P.galleon; AGetChar ();//read. atCIN >>P.sickle; - GetChar (); -CIN >>P.knut; - -CIN >>A.galleon; - GetChar (); inCIN >>A.sickle; - GetChar (); toCIN >>A.knut; + - if(A.knut <p.knut) the { *Temp. Knut = in+ A.knut-P.knut; $a.sickle--;Panax Notoginseng } - Else theTemp. Knut = A.knut-P.knut; + A if(A.sickle <p.sickle) the { +Temp. Sickle = -+ A.sickle-P.sickle; -a.galleon--; $ } $ Else -Temp. Sickle = A.sickle-P.sickle; - theTemp. Galleon = A.galleon-P.galleon; - Wuyi if(temp. Galleon <0) the { - if(temp. Knut >0) Wu { -Temp. Knut-= in; AboutTemp. sickle++; $ } - - if(temp. Sickle >0) - { ATemp. Sickle-= -; +Temp. galleon++; the } - $cout << Temp. Galleon <<"."<<-temp. Sickle <<"."<<-temp. Knut <<Endl; the } the Else thecout << Temp. Galleon <<"."<< temp. Sickle <<"."<< temp. Knut <<Endl; the - return 0; in}
PAT B. 1037 find change at Hogwarts C + +