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

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

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
• 1

Sample Output:

14.1.28


题意


这道题是要我们去模拟进制运算,给定两个数,格式是 a.b.c ,c 的范围在 [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;
}
目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
303 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
382 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
363 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
331 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
257 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
304 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
511 0
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
759 12
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)