PAT_A 1058 A+B in Hogwart

PAT_A 1058 A+B in Hogwart

分析

本题和 PAT_B 1037 在霍格沃茨找零钱 除了加减不同外几乎完全一致,思路也是相似的,即将输入的数据转换为最小的单位(Knut)后相加,再转换回要求的格式进行输出即可

题目的描述

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<bits/stdc++.h>

using namespace std;

int main(){
    long long pg,ps,pk,ag,as,ak,he=0;

    scanf("%lld.%lld.%lld %lld.%lld.%lld",&pg,&ps,&pk,&ag,&as,&ak);
    pk += pg*17*29 + ps*29;
    ak += ag*17*29 + as*29;
    he = ak+pk;
    if(he<0){
        he = -he;
        cout<<'-';
    }
    cout<<he/17/29<<'.'<<(he%(17*29))/29<<'.'<<(he%(17*29))%29<<endl;
    return 0;
}

posted @ 2022-01-24 22:49  ghosteq  阅读(23)  评论(0编辑  收藏  举报