1058 A+B in Hogwarts
题目详情 - 1058 A+B in Hogwarts (pintia.cn)
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,107], 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 • 1
Sample Output:
14.1.28
题意
这道题是要我们去模拟进制运算,给定两个数,格式是 a.b.c ,c 的范围在 [0,29) ,b 的范围在 [0,17) ,也就是说 c 每达到 29 就会往 b 进一位即二十九进制,而 b 每达到 17 就会往 a 进一位即十七进制。
现在就要我们通过这个进制要求去计算两个给定格式的数之和,并且输出最终结果。
思路
- 将两个数分别用
a,b,c
输入进来,然后先将对应位相加。 - 然后处理进位情况,由于进位是从后往前进,所以我们先要计算最后一位即
c
的进位情况,将c
进的位加到b
上,然后再处理b
的进位情况并加到a
上。 - 最后输出结果。
代码
#include<bits/stdc++.h> using namespace std; int main() { int a1, b1, c1, a2, b2, c2; scanf("%d.%d.%d %d.%d.%d", &a1, &b1, &c1, &a2, &b2, &c2); a1 += a2, b1 += b2, c1 += c2; b1 += c1 / 29, c1 %= 29; a1 += b1 / 17, b1 %= 17; printf("%d.%d.%d", a1, b1, c1); return 0; }