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

分析:
本题考查进制转换,由于每次转换的进制不同,此处提供两种解法。

解法1先计算Knut位,并计算进位add,然后计算Sickle位,并计算进位add,最后计算Galleon位,计算过程中所有数(包括和值)的最大值是2*107+1,在int范围内,所以数据类型定义为int,此解法分析过程没发现错误,但是最后一个测试点过不了,暂时还未找出原因,先将解法贴出,日后回顾时再进一步思考。

解法2将所有位的数先全部转换为Knut,然后将和值的Knut化成标准型,计算过程中和值最大值是2(2107*17*29+16*29+28)=1.972e+010,此值超出了int的范围,因此数据类型定义为long long.

解法2:

#include
using namespace std;
int main(){
    long long a,b,c,d,e,f;
    scanf("%lld.%lld.%lld %lld.%lld.%lld",&a,&b,&c,&d,&e,&f);
    long long sum=(a+d)*17*29+(b+e)*29+c+f;
    printf("%lld.%lld.%lld",sum/(17*29),(sum-sum/(17*29)*17*29)/29,sum%29);
    return 0;
}

解法1

#include
#include 
using namespace std;
int main(){
    int a[6];
    scanf("%d.%d.%d %d.%d.%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5]);
    int add=0;
    int b[3];
    b[2]=a[2]+a[5];
    if(b[2]>=29){
        add=1;
        b[2]%=29;
    } 
    b[1]=a[1]+a[4]+add;
    if(b[1]>=17){
        add=1;
        b[1]%=17;
    }
    b[0]=a[0]+a[3]+add;
    printf("%d.%d.%d\n",b[0],b[1],b[2]);
    return 0;
} 

你可能感兴趣的:(1058 A+B in Hogwarts (20 分)(进制转换))