PAT 乙级 1037 在霍格沃茨找零钱(20)C++版

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

难点:如何将三个整数用A.B.C形式输入读取成功
思路2难点:输出处,必须满足固定的输出方式

思路1:先将P和A的2钱数全部转换为最小计量数,然后再作差,再转换为三级单位表示的形式
思路2:自接加减补进

思路1代码:
 1 // 1037.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include<iostream>
 6 
 7 using namespace std;
 8 
 9 class money
10 {
11 public:
12     int Galleon;
13     int Sickle;
14     int Knut;
15 
16     int get_knut();
17     money get_money(int knut);
18     void show_money(money m,bool b);
19 };
20 
21 int main()
22 {
23     money P, A,temp;
24 
25     cin >> P.Galleon;
26     getchar();//读取 . 下同
27     cin >> P.Sickle;
28     getchar();
29     cin>> P.Knut;
30 
31     cin >> A.Galleon;
32     getchar();
33     cin >> A.Sickle;
34     getchar();
35     cin >> A.Knut;
36 
37     //多个函数综合到一起
38     temp.show_money(temp.get_money(A.get_knut() - P.get_knut()), (A.get_knut() - P.get_knut()) >= 0?1:0);
39 }
40 
41 //计算总knut
42 int money::get_knut()
43 {
44     return 17 * 29 * this->Galleon + 29 * this->Sickle + this->Knut;
45 }
46 
47 //将返回一个类的对象,存储差价
48 money money::get_money(int knut)
49 {
50     this->Galleon = knut / 17 / 29;
51     knut -= Galleon * 17 * 29;
52     this->Sickle = knut/29;
53     knut -= Sickle * 29;
54     this->Knut = knut;
55 
56     return *this;
57 }
58 
59 //按类形式输出结果
60 void money::show_money(money m,bool b)
61 {
62     if (b)
63         cout << m.Galleon << "." << m.Sickle << "." << m.Knut << endl;
64     else
65         cout << m.Galleon << "." << -m.Sickle << "." << -m.Knut << endl;
66 }

当然第三十八行可读性非常差,不过也只是将几个函数综合到一起,看起来比较复杂,其实内容很简单

思路2代码:

 1 // 1037_1.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include<iostream>
 6 
 7 using namespace std;
 8 
 9 class money
10 {
11 public:
12     int Galleon;
13     int Sickle;
14     int Knut;
15 };
16 
17 int main()
18 {
19     money P, A, temp;
20 
21     cin >> P.Galleon;
22     getchar();//读取 . 下同
23     cin >> P.Sickle;
24     getchar();
25     cin >> P.Knut;
26 
27     cin >> A.Galleon;
28     getchar();
29     cin >> A.Sickle;
30     getchar();
31     cin >> A.Knut;
32 
33     if (A.Knut < P.Knut)
34     {
35         temp.Knut = 29+ A.Knut - P.Knut;
36         A.Sickle--;
37     }
38     else
39         temp.Knut = A.Knut - P.Knut;
40 
41     if (A.Sickle < P.Sickle)
42     {
43         temp.Sickle =17+ A.Sickle - P.Sickle;
44         A.Galleon--;
45     }
46     else
47         temp.Sickle = A.Sickle - P.Sickle;
48 
49     temp.Galleon = A.Galleon - P.Galleon;
50 
51     if (temp.Galleon < 0)
52     {
53         if (temp.Knut > 0)
54         {
55             temp.Knut -= 29;
56             temp.Sickle++;
57         }
58 
59         if (temp.Sickle > 0)
60         {
61             temp.Sickle -= 17;
62             temp.Galleon++;
63         }
64 
65         cout << temp.Galleon << "." << -temp.Sickle << "." << -temp.Knut << endl;
66     }
67     else
68         cout << temp.Galleon << "." << temp.Sickle << "." << temp.Knut << endl;
69 
70     return 0;
71 }
原文地址:https://www.cnblogs.com/cdp1591652208/p/7134117.html