A1058. A+B in Hogwarts (20)

本文介绍了一种基于哈利波特系列小说中魔法世界货币系统的计算器实现方法。通过三种不同版本的C/C++代码,展示了如何将两种魔法货币(以加隆、西可和纳特为单位)相加并保持其原有格式输出。此计算器采用模块化设计,易于理解和修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题目大意

29个Knut换一个 Sickle
17个Sickle换一个Galleon。
三个版本代码:

Version 1

写的好复杂,好长啊。。。太菜了,还是参考version2吧

#include <stdio.h>
using namespace std;
int main(){
	int array1[3],array2[3];
	scanf("%d.%d.%d %d.%d.%d",&array1[0],&array1[1],&array1[2],&array2[0],&array2[1],&array2[2]);
	int ans[3]={0};
	int ch[3] = {1000001,17,29};
	int temp[3]={0};
	for(int i=2;i>=0;i--){
		if((array1[i]+array2[i])>ch[i]){
			temp[i-1]++;
		}
		temp[i]+=(array1[i]+array2[i])%ch[i];
	}
	printf("%d.%d.%d",temp[0],temp[1],temp[2]);
	return 0;	
}

Version 2

#include <stdio.h>
using namespace std;
int main(){
	int array1[3],array2[3];
	scanf("%d.%d.%d %d.%d.%d",&array1[0],&array1[1],&array1[2],&array2[0],&array2[1],&array2[2]);
	int temp[3]={0};
	int carry=0;
	temp[2]=(array1[2]+array2[2])%29;
	carry = (array1[2]+array2[2])/29;
	temp[1] =(array1[1]+array2[1]+carry)%17;
	carry=(array1[1]+array2[1]+carry)/17;
	temp[0]= (array1[0]+array2[0]+carry);
	printf("%d.%d.%d",temp[0],temp[1],temp[2]);
	return 0;	
}

Version 3:柳神,看了人家的代码,感觉自己写的代码就是渣。自愧不如。

#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 num = c + b * 29 + a * 29 * 17 + f + e * 29 + d * 29 * 17;
    long long g = num / (17 * 29);
    num = num % (17 * 29);
    printf("%lld.%lld.%lld", g, num / 29, num % 29);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wvdon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值