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

Sample Output:

14.1.28
#include <iostream>
#include <algorithm>
#include <vector>


using namespace std;




int main(){

    char str1[100],str2[100];
    long long g1,g2,s1,s2,k1,k2;

    long long res=0,num1,num2;

    scanf("%s",str1);
    scanf("%s",str2);

    sscanf(str1,"%lld.%lld.%lld",&g1,&s1,&k1);
    sscanf(str2,"%lld.%lld.%lld",&g2,&s2,&k2);

    num1 = k1+s1*29+g1*29*17;
    num2 = k2+s2*29+g2*29*17;

    res=num1+num2;

    printf("%lld.%lld.%lld\n",res/(29*17),(res/29)%17,res%29);

    return 0;
}







这里注意一下,之前是int类型有一个点通不过,后来改为long long 类型之后,此题可得满分。int的g*17*s*29*k所得的最大结果超过了整型,使用long long类型。