# SPOJ Problem Set (classical) 6. Simple Arithmetics

libraco
• 760

``````def Add(a, b):
l = []
alen = len(a)
blen = len(b)
result = str(int(a) + int(b))
relen = len(result)
llen = alen > blen and alen or blen+1
llen = relen > llen and relen or llen
l.append(' ' * (llen - alen) + a)
l.append(' ' * (llen - blen - 1) + '+' + b)
l.append('-' * llen)
l.append(' ' * (llen - relen) + result)
List.append(l)

def Sub(a, b):
l = []
alen = len(a)
blen = len(b)
result = str(int(a) - int(b))
relen = len(result)
llen = alen > blen and alen or blen+1
l.append(' ' * (llen - alen) + a)
l.append(' ' * (llen - blen - 1) + '-' + b)
l.append('-' * llen)
l.append(' ' * (llen - relen) + result)
List.append(l)

def Multi(a, b):
l = []
alen = len(a)
blen = len(b)
result = str(int(a) * int(b))
relen = len(result)
llen = relen > blen+1 and relen or blen+1
_len_bove = alen > blen and alen or blen+1
_len_below = len(str(int(a) * int(b[-1])))
_len = _len_bove > _len_below and _len_bove or _len_below
l.append(' ' * (llen - alen) + a)
l.append(' ' * (llen - blen - 1) + '*' + b)
l.append(' ' * (llen - _len) + '-' * _len)
for i in range(blen):
cpt = str(int(a) * int(b[-i-1]))
l.append(' ' * (llen - len(cpt) - i) + cpt)
if blen != 1:
l.append('-' * llen)
l.append(' ' * (llen - relen) + result)
List.append(l)

n = input()
List = []
for i in range(n):
string = raw_input()
if '+' in string:
a, b = string.split('+')
elif '-' in string:
a, b = string.split('-')
Sub(a, b)
elif '*' in string:
a, b = string.split('*')
Multi(a, b)

for l in List:
for i in l:
print i
print
``````

1 个回答
✓ 已被采纳

http://contest.felk.cvut.cz/00cerc/solved/arith.in
http://contest.felk.cvut.cz/00cerc/solved/arith.out
（终于知道为什么这道题AC率这么低了！改了十几次都是小地方没注意到有木有！）

1.减法的破折号长度取决于第二个操作数和结果数，并非是操作数和结果数的最大值。

``````1000
-1
---
999
``````

2.乘法的结果数上破折号的长度仅取决于它自己，不一定和第一个破折号长度相等。

``````     1
*12345
------
5
4
3
2
1
-----
12345
``````

###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进