IEEEXtreme 10.0 - Full Adder
这是 meelo 原创的 IEEEXtreme极限编程大赛题解
Xtreme 10.0 - Full Adder
题目来源 第10届IEEE极限编程大赛
https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/full-adder
We would like your help to create a basic adder. However, this adder, should work in any base, with any set of symbols.
Input Format
The first line of input contains an integer b, a space, and a list of b symbols that make up the base. The symbols are listed in order from the least significant symbol to the most significant symbol. In other words, the first symbol listed corresponds to 0, the second corresponds to 1, etc. These symbols can be numbers, uppercase letters, or lowercase letters.
The remaining lines contain the addition problem to be solved, as shown in the sample input and output. The operands will be non-negative numbers expressed in the given base. Note that the last line contains question marks which must be replaced with the correct value.
Constraints
2 ≤ b ≤ 62
The numbers to be added can contain up to 107 symbols.
Output Format
The first four lines of output should be identical to the input. The last line should contain the solution to the problem, with the answer aligned appropriately.
Sample Input
10 0123456789
752
+76045
------
???
Sample Output
10 0123456789
752
+76045
------
76797
Explanation
The first sample corresponds to a normal base-10 addition problem.
Additional sample problems are available if you click on the Run Code button.
The second sample problem has the following input:
10 wj8Ma04HJg
H
+8J4J
-----
???
This is a base-10 problem with different symbols. H corresponds to the digit 7 and 8J4J is the number 2868. When adding these numbers, the result is 2875, which is represented as 8JH0 in the given base. Thus the expected output is:
10 wj8Ma04HJg
H
+8J4J
-----
8JH0
题目解析
题目要求实现一个加法,但是字符集是输入的字符集。
需要建立一个字典,表示字符到值得映射;原始表示字符集的字符串,就可以表示值到字符的映射。
加法是从后往前进行的,Python中用range(len(add1)-1, 0, -1)就可以方便的实现。
一个小技巧是在字典中加入一个从空格到0的映射,就可以解决两个字符串长度不同的问题。
还需要注意,输出的最后一行之前需要加上合适长度的空格。
程序
Python3
line1 = input()
add1 = input()
add2 = input()
line4 = input() base, symbols = line1.split(' ')
m = {}
m[' '] = 0 # treat preceding space as 0 base = int(base) # character -> value
for i, c in enumerate(symbols):
m[c] = i total = 0
addin = 0
result = '' # add from back to front
for i in range(len(add1)-1, 0, -1):
total = m[add1[i]] + m[add2[i]] + addin
addin = total // base
result += symbols[total % base] print(line1)
print(add1)
print(add2)
print(line4)
print(' '*(len(add1)-len(result)) + result[::-1])
博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址
IEEEXtreme 10.0 - Full Adder的更多相关文章
- IEEEXtreme 10.0 - Inti Sets
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...
- IEEEXtreme 10.0 - Painter's Dilemma
这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...
- IEEEXtreme 10.0 - Ellipse Art
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...
- IEEEXtreme 10.0 - Counting Molecules
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- IEEEXtreme 10.0 - Checkers Challenge
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- IEEEXtreme 10.0 - Game of Stones
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...
- IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...
- IEEEXtreme 10.0 - N-Palindromes
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...
- IEEEXtreme 10.0 - Mysterious Maze
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mysterious Maze 题目来源 第10届IEEE极限编程大赛 https://www.hacker ...
随机推荐
- 第4章-Vue.js 交互及实例的生命周期
一.学习目标 了解实例生命周期的过程 理解钩子函数的作用 掌握Vue.js过滤器的使用方法 (重点) 能够使用网络请求进行前后端交互 (重点.难点) 二.交互的基本概念 2.1.前端和后端的概念 说明 ...
- javaFX8主要特性
javaFX8主要特性 JavaAPIs:javaFX是用Java代码写的库,包含一系列的类和接口.这个APIs可以友好的替换java虚拟机语言,比如:JRuby和Scala. FXML and 场景 ...
- AndroidStudio环境搭建
简单记录一下AS的环境搭建过程,包括SVN的使用. 一.下载和安装JDK 地址:http://www.oracle.com/technetwork/java/javase/downloads/inde ...
- centos7下安装mysql5.7.24
第一步:下载rpm包 sudo wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-rel ...
- 使用git拉取github上的项目
一. 安装Git 去Git官网,下载安装包,一路点next,默认安装. 安装之后,在空白处右键,菜单显示有 Git GUI Here 和 Git Bash Here ,表示Git安装成功. 二. 配置 ...
- JAVA多线程提高二:传统线程的互斥与同步&传统线程通信机制
本文主要是回顾线程之间互斥和同步,以及线程之间通信,在最开始没有juc并发包情况下,如何实现的,也就是我们传统的方式如何来实现的,回顾知识是为了后面的提高作准备. 一.线程的互斥 为什么会有线程的互斥 ...
- Stat2—主成分分析(Principal components analysis)
最近在猛撸<R in nutshell>这本课,统计部分涉及的第一个分析数据的方法便是PCA!因此,今天打算好好梳理一下,涉及主城分析法的理论以及R实现!come on…gogogo… 首 ...
- 【BZOJ】1901: Zju2112 Dynamic Rankings
[题意]带修改的查询区间第k小 [算法]树状数组套可持久化线段树 [题解]对于树状数组上的每个节点,维护可持久化权值线段树(节点为权值),从而达到查询前缀和的目的. 对于每次修改,在待修改线段树基础上 ...
- 【BZOJ】1176: [Balkan2007]Mokia
[题意]n*n的矩阵,初始值为0(题面有误),m次操作,增加一个格子的权值,或查询子矩阵和.n<=2*10^6.(m应该较题面所述偏大). [算法]CDQ分治(算法知识见数据结构) [题解]三维 ...
- js面向对象的几种常见写法
下面是一个简单的js对象:定义Circle类,拥有成员变量r,常量PI和计算面积的成员函数area(),常用为第一种和第三种. 1.工厂方式 var Circle = function() { var ...