半灵桑 发表于 2023-10-8 09:36:10

如何实现加法

军火展示:
def plus(a, b):
    return (str(a) + '+' + str(b) + '=' + str(abs(a) + abs(b))) if a > 0 and b > 0 else ('(' + str(a) + ') + (' + str(b) + ')=' + str(-(abs(a) + abs(b)))) if a < 0 and b < 0 else (str(a) + ' + (' + str(b) + ')=' + str(a - abs(b))) if a > 0 and b < 0 and abs(a) > abs(b) else (str(a) + ' + (' + str(b)+ ')=' + str(-(abs(b) - a))) if a > 0 and b < 0 else ('(' + str(a) + ') +' + str(b) + '=' + str(-(abs(a) - b))) if a < 0 and b > 0 and abs(a) > abs(b) else ('(' + str(a) + ') +' + str(b) + '=' + str(b - abs(a))) if a < 0 and b > 0 else (str(a) + '+' + str(b) + '=' + str(b)) if a == 0 else (str(a) + '+' + str(b) + '=' + str(a))
print(*plus(1,1))

只有三行,非常简洁,遥遥领先于C++(
页: [1]
查看完整版本: 如何实现加法