PAT A1058 A+B in Hogwarts

PAT A1058 A+B in Hogwarts

题目描述:

  If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- as Hagrid explained it to Harry, "Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it's easy enough." Your job is to write a program to compute A+B where A and B are given in the standard form of Galleon.Sickle.Knut (Galleon is an integer in [0,10​7​​], Sickle is an integer in [0, 17), and Knut is an integer in [0, 29)).

  Input Specification:
  Each input file contains one test case which occupies a line with A and B in the standard form, separated by one space.

  Output Specification:
  For each test case you should output the sum of A and B in one line, with the same format as the input.

  Sample Input:
  3.2.1 10.16.27

  Sample Output:
  14.1.28

参考代码:

 1 /****************************************************
 2 PAT A1058 A+B in Hogwarts
 3 ****************************************************/
 4 #include <iostream>
 5 
 6 using namespace std;
 7 
 8 const int RATIO[2] = { 17, 29 };  //存储进制信息
 9 
10 int main() {
11     char point;
12     int num[3][3]{ 0 };           //num[0], num[1], num[2]分别代表a, b, a + b
13     
14     //读入加数a, b
15     for (int i = 0; i < 2; ++i) {
16         cin >> num[i][0] >> point >> num[i][1] >> point >> num[i][2];
17     }
18 
19     //计算a + b
20     int carry = 0;
21     for (int i = 2; i > 0; --i) {
22         num[2][i] = (num[0][i] + num[1][i] + carry) % RATIO[i - 1];
23         carry = (num[0][i] + num[1][i] + carry) / RATIO[i - 1];
24     }
25     num[2][0] = num[0][0] + num[1][0] + carry;
26 
27     //输出a + b
28     for (int i = 0; i < 3; ++i) {
29         cout << num[2][i];
30         if (i != 2) cout << point;
31     }
32 
33     return 0;
34 }

注意事项:

  1:本题和乙级中的1037 在霍格沃茨找零钱:https://www.cnblogs.com/mrdragon/p/11402980.html相似,如果按照B1037中的做法的话需要注意应该将Galleon、allByKunt的类型改为long long,但是这种算法会大大增加程序的运行时间。

posted @ 2019-08-24 00:06  多半是条废龙  阅读(147)  评论(0)    收藏  举报