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*(2*107*17*29+16*29+28)=1.972e+010,此值超出了int的范围,因此数据类型定义为long long.

解法2:

#include<iostream>
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<iostream>
#include <cmath>
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;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值