PAT (Advanced Level) Practice 1058 A+B in Hogwarts (20 分) 凌宸1642
PAT (Advanced Level) Practice 1058 A+B in Hogwarts (20 分) 凌宸1642
题目描述:
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)).
译:如果你是哈利·波特的粉丝,你就会知道魔法世界有自己的货币体系——正如海格对哈利解释的那样:“17个银西可兑换1个加隆,29个纳特兑换1个银西,很简单。”你的工作是写一个程序来计算 A +B 其中 A 和 B 是以标准形式给出的 Galleon.Sickle.Knut
(Galleon是[0,107]中的整数,Sickle 是[0 , 17) 中的整数,Knut 是[ 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.
译:每个测试文件包含一个测试用例,在一行中A 和 B 按标准的形式输入,并用空格分隔。
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.
译:对于每个测试用例,你应该按输入的格式输出 A + B 的和 。
Sample Input (样例输入):
3.2.1 10.16.27
Sample Output (样例输出):
14.1.28
The Idea:
很简单!!就是从 Knut
开始相加,如果 Kunt
大于等于 29
则需要进位给 Sickle
,然后 Sickle
相加,有进位需要进位给 Galleon
然后再按格式输出即可。
The Codes:
#include<bits/stdc++.h>
using namespace std;
int g1 , s1, k1 , g2 , s2, k2 , g3 , s3, k3 ;
int main(){
scanf("%d.%d.%d %d.%d.%d" , &g1 , &s1 , &k1 , &g2 , &s2, &k2);
k3 = (k1 + k2) % 29 ;
s3 = (s1 + s2 + (k1 + k2) / 29) % 17 ;
g3 = g1 + g2 + (s1 + s2 + (k1 + k2) / 29) / 17 ;
printf("%d.%d.%d\n" , g3 , s3 , k3) ;
return 0;
}