这是 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的更多相关文章

  1. IEEEXtreme 10.0 - Inti Sets

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  2. IEEEXtreme 10.0 - Painter's Dilemma

    这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...

  3. IEEEXtreme 10.0 - Ellipse Art

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...

  4. IEEEXtreme 10.0 - Counting Molecules

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  5. IEEEXtreme 10.0 - Checkers Challenge

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  6. IEEEXtreme 10.0 - Game of Stones

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...

  7. IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...

  8. IEEEXtreme 10.0 - N-Palindromes

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...

  9. IEEEXtreme 10.0 - Mysterious Maze

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mysterious Maze 题目来源 第10届IEEE极限编程大赛 https://www.hacker ...

随机推荐

  1. Linux下的tar压缩解压缩命令详解(转)

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  2. python如何优雅判断类型

    http://note.youdao.com/noteshare?id=6f3a7963efc57b5d0b1c712654d100c6

  3. 【题解】【LibreOJ Beta Round #5】游戏 LOJ 531 基环树 博弈论

    Prelude 题目链接:萌萌哒传送门♪(^∇^*) Subtask 1 & 2 这是什么鬼题面... 首先要看出,这就是一个基环树博弈. 具体题意:给出一个基环内向树,一个棋子初始在\(1\ ...

  4. Qt ------ 断开某对信号与槽的connect

    QMetaObject::Connection dis; dis = connect(this,&TcpSocket::readyRead,this,&TcpSocket::readD ...

  5. AES和RSA混合加密技术在网络数据传输中的应用

    原文:http://www.fx361.com/page/2017/0110/519967.shtml          摘要:文章通过分析和比较AES加密算法和RsA加密算法的实现过程和各自的特点, ...

  6. [Java多线程]-ThreadLocal源码及原理的深入分析

    ThreadLocal<T>类:以空间换时间提供一种多线程更快捷访问变量的方式.这种方式不存在竞争,所以也不存在并发的安全性问题. //-------------------------- ...

  7. Hadoop 遇到的问题集

    1. Mac 使用ssh命令无法成功 ssh localhost,但是可以ssh其他的 可能原因:ssh服务未启动 解决方法: 1.启动sshd服务: sudo launchctl load -w / ...

  8. 树的性质和dfs的性质 Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E

    http://codeforces.com/contest/782/problem/E 题目大意: 有n个节点,m条边,k个人,k个人中每个人都可以从任意起点开始走(2*n)/k步,且这个步数是向上取 ...

  9. elasticsearch ik中文分词器的安装配置使用

    安装步骤  https://github.com/medcl/elasticsearch-analysis-ik 以插件形式安装: [elsearch@localhost elasticsearch- ...

  10. flume监控一个linux指定的一个文件夹的文件信息

    1.编辑一个配置文件 flume-app.conf  拷贝至fulme的安装目录的conf下 # The configuration file needs to define the sources, ...