1058 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], 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

题意:

  按照特殊的进制实现加法运算。

思路:

  模拟。

Code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 vector<int> praseString(string str) {
 6     int pos1 = str.find('.');
 7     int pos2 = str.find('.', pos1 + 1);
 8     string num1, num2, num3;
 9     num1 = str.substr(0, pos1);
10     num2 = str.substr(pos1 + 1, pos2 - pos1 - 1);
11     num3 = str.substr(pos2 + 1);
12     vector<int> res;
13     res.push_back(stoi(num1));
14     res.push_back(stoi(num2));
15     res.push_back(stoi(num3));
16     return res;
17 }
18 
19 int main() {
20     string num1, num2;
21     cin >> num1 >> num2;
22     vector<int> v1 = praseString(num1);
23     vector<int> v2 = praseString(num2);
24     int galleon, sickle, knut, add;
25     knut = (v1[2] + v2[2]) % 29;
26     add = (v1[2] + v2[2]) / 29;
27     sickle = (v1[1] + v2[1] + add) % 17;
28     add = (v1[1] + v2[1] + add) / 17;
29     galleon = v1[0] + v2[0] + add;
30     cout << galleon << "." << sickle << "." << knut << endl;
31     return 0;
32 }

 

posted @ 2020-05-06 21:21  Veritas_des_Liberty  阅读(186)  评论(0编辑  收藏  举报