1. 程式人生 > >PAT 1058 A+B in Hogwarts (20 分)

PAT 1058 A+B in Hogwarts (20 分)

1058 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

710​^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

解析

我是把Galleon,Sickles全部轉換成Knut。再進行相加。但是這樣會產生溢位。 Galleon最大值為10710^7。轉換成Knut為4930000000。而int的範圍是2147483648-1。所以要用long long儲存。 Code:

#include<cstdio>
int main()
{
	long long Ga,Sa, Ka,suma=0;
	long long Gb,Sb, Kb,sumb=0;
	scanf("%lld.%lld.%lld",&
Ga,&Sa,&Ka ); scanf("%lld.%lld.%lld",&Gb,&Sb,&Kb); suma = Ga * 17 * 29 + Sa * 29 + Ka; sumb = Gb * 17 * 29 + Sb * 29 + Kb; long long sum = suma + sumb; long long G = sum / 17 / 29; long long S = sum/29-G*17; long long K = sum - G * 17 * 29 - S * 29; printf("%d.%d.%d",G,S,K); /* 17 silver Sickles = 1 Galleon 29 Knut = 1 silver Sickles */ }