【PAT甲级 - C++题解】1058 A+B in Hogwarts

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1153 Decode Registration Card of PAT (pintia.cn)
🔑中文翻译:解码PAT准考证
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

1058 A+B in Hogwarts

题目详情 - 1058 A+B in Hogwarts (pintia.cn)

中文翻译

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
题意

这道题是要我们去模拟进制运算,给定两个数,格式是 a.b.cc 的范围在 [0,29)b 的范围在 [0,17) ,也就是说 c 每达到 29 就会往 b 进一位即二十九进制,而 b 每达到 17 就会往 a 进一位即十七进制。

现在就要我们通过这个进制要求去计算两个给定格式的数之和,并且输出最终结果。

思路
  1. 将两个数分别用 a,b,c 输入进来,然后先将对应位相加。
  2. 然后处理进位情况,由于进位是从后往前进,所以我们先要计算最后一位即 c 的进位情况,将 c 进的位加到 b 上,然后再处理 b 的进位情况并加到 a 上。
  3. 最后输出结果。
代码
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int a1, b1, c1, a2, b2, c2;
    scanf("%d.%d.%d %d.%d.%d", &a1, &b1, &c1, &a2, &b2, &c2);

    a1 += a2, b1 += b2, c1 += c2;
    b1 += c1 / 29, c1 %= 29;
    a1 += b1 / 17, b1 %= 17;

    printf("%d.%d.%d", a1, b1, c1);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值