这是 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. 【codechef】Children Trips

    Portal -->CC_Children Trips Solution (英文题解看得真爽qwq不过写的好详细啊ovo) 首先这题有一个很重要的条件就是边权是\(1\)或者\(2\),所以虽然 ...

  2. 2018-2019 ACM-ICPC 焦作赛区 部分题解

    题目链接:https://codeforces.com/gym/102028 B. Ultraman vs. Aodzilla and Bodzilla 题意: 两只怪兽,它们的生命和攻击分别为hpA ...

  3. LINUX安全加固操作

    1.禁止Ctrl-Alt-Delete组合键重启系统 vi /etc/inittab #ca::ctrlaltdel:/sbin/shutdown -t3 -r now 如果还存在下面的文件,则需要注 ...

  4. ubuntu如何杀死进程

    一.得到所有进程 先用命令查询出所有进程 ps -ef 二.杀死进程 我们使用ps -ef命令之后,就会得到一些列进程信息,有进程pid什么的,如果你要杀死莫个进程的话,直接使用命令   kill   ...

  5. MingW和MSVC默认的编码方式不一样

    同一份源代码,源文件编码格式为UTF-8: string tmp = "我"; ;i<tmp.size();++i) { printf("%0x ",tm ...

  6. Android Studio 打包自定义apk文件名

    使用Android Studio打包的时候,我们有时候需要自定义apk的文件名,在此记录一下. 在app的build.gradle中,根节点下使用关键词def声明一个全局变量,用于获取打包的时间,格式 ...

  7. Python学习笔记(九)返回函数

    摘抄:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014318352367 ...

  8. eclipse中修改svn用户名和密码

    开发中有时候用公共的电脑提交一些代码,eclipse没有专门的切换svn账户的功能.查阅资料得出解决办法: 1. 查看你的Eclipse 中使用的是什么SVN Interface  windows & ...

  9. ⑦ 设计模式的艺术-13.代理(Proxy)模式

    为什么需要代理模式 中介隔离作用:在某些情况下,一个客户类不想或者不能直接引用一个委托对象,而代理类对象可以在客户类和委托对象之间起到中介的作用,其特征是代理类和委托类实现相同的接口. 开闭原则,增加 ...

  10. 《JavaScript 实战》:实现图片幻滑动展示效果

    滑动展示效果主要用在图片或信息的滑动展示,也可以设置一下做成简单的口风琴(Accordion)效果.这个其实就是以前写的图片滑动展示效果的改进版,那是我第一篇比较受关注的文章,是时候整理一下了. 有如 ...