A1058 A+B in Hogwarts (20 point(s))

不同进位数相加

1. 原文

原题

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:

1
3.2.1 10.16.27

Sample output:

1
14.1.28

2. 解析

不同进位 十位17 个位29

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
#include<cstdio>
int main()
{
long long g1,s1,k1,g2,s2,k2;
scanf("%lld.%lld.%lld %lld.%lld.%lld",&g1,&s1,&k1,&g2,&s2,&k2);
long long a=g1*17*29+s1*29+k1;
long long b=g2*17*29+s2*29+k2;
long long sum=a+b;
printf("%lld.%lld.%lld\n",sum/(17*29),sum/29%17,sum%29);
return 0;
}

数字可能溢出,应该用long long

Way2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<cstdio>
int num1[3],num2[3];
int ans[3]={};
int main(){
scanf("%d.%d.%d %d.%d.%d",
&num1[0],&num1[1],&num1[2],
&num2[0],&num2[1],&num2[2]);
int temp = num1[2]+num2[2];
ans[2]=temp%29;
temp/=29;
temp = num1[1]+num2[1]+temp;
ans[1]=temp%17;
temp/=17;
ans[0]=temp+num1[0]+num2[0];
printf("%d.%d.%d\n",ans[0],ans[1],ans[2]);
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1058/
  • 发布时间: 2019-09-03 12:33
  • 更新时间: 2021-10-29 14:02
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!