PAT A1058 A+B in Hogwarts (20分)
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
以下是AC的代码:
#include<cstdio>
#include<cstring>
int main(void){
long long int ga1,si1,kn1,ga2,si2,kn2;
scanf("%lld.%lld.%lld %lld.%lld.%lld",&ga1,&si1,&kn1,&ga2,&si2,&kn2);
long long int total1,total2;
total1=ga1*17*29+si1*29+kn1;
total2=ga2*17*29+si2*29+kn2;
long long int ans=total1+total2;
long long int ga3,si3,kn3;
kn3=ans%29;
ans=ans/29;
si3=ans%17;
ans/=17;
ga3=ans;
printf("%lld.%lld.%lld",ga3,si3,kn3);
return 0;
}
思路:
跟PAT B1037 在霍格沃茨找零钱 (20分)非常相似,B1037是求相减结果,这道题是相加的结果,采用同样的思路可以解决。但是,一定要注意:这道题计算过程中可能会溢出,需要用long long int才能保证数据不出错。