我在使用 Python 2 计算任何乘法问题中的进位数量时遇到问题。我试图调整我已经编写的程序,该程序计算任何加法问题中的进位,但我似乎仍然无法使其工作。我目前正在学习 Python 的基础知识,所以我使用的是非常简单的东西。任何有关如何将此程序转换为适用于乘法的指示都将不胜感激!
这是我的加法程序计数进位:
if len(str(x)) != len(str(y)):
if len(x) > len(y):
while len(x) > len(y):
y = '0' + y
else:
while len(y) > len(x):
x = '0' + x
z = int(x) + int(y)
counter = 0
carries = 0
i = len(str(x))
while i > 1:
i -= 1
added = int((x[i])) + int((y[i])) + counter
if added > 9:
carries +=1
counter = 1
else:
counter = 0
print str(x), '+', str(y), '=', z
print 'Number of carries: ', str(carries)
请您参考如下方法:
我做出了与@TehTechGuy 相同的假设,尽管我处理问题的方式不同。我选择使用递归方法而不是迭代方法,因为它对于这类问题更自然。
如果我们统计的是加法阶段发生的进位,那么需要将计算加法进位的逻辑封装在一个方法中,如下:
def count_addition_carries_rec(nums, answer=0, carries=0):
dig_count = max(len(str(nums[0])), len(str(answer)))
carries_list = [0]
new_answer = ''
# convert to string, apply left-padding, and reverse
rnums = [str(x).zfill(dig_count)[::-1] for x in [nums[0], answer]]
for i in range(dig_count):
dig_sum = str(sum([int(num[i]) for num in rnums]) + carries_list[i])
if i < dig_count - 1:
new_answer = dig_sum[-1] + new_answer
carries_list.append(int(dig_sum[:-1].zfill(1)))
else:
new_answer = dig_sum + new_answer
carries_list = [car for car in carries_list if car != 0]
if len(nums) == 1:
# If this is the last number in the list,
# return the answer and the number of carries that
# occurred in the current as well as previous operations.
return int(new_answer), carries + len(carries_list)
else:
# if there are more numbers in the list,
# repeat the operation with a sublist, consisting of the next
# number onwards, passing the current sum (new_answer) and
# the current count of carries
return count_addition_carries_rec(nums[1:],
new_answer,
carries + len(carries_list))
count_addition_carries_rec 方法是通用的,因为它可以接受 2 个以上的整数。 nums 参数是一个列表,该方法期望其长度为 2 或更多。
计算乘法进位的方法如下:
def count_multiplication_carries_rec(num1, num2, answer=0, carries=0):
num1, num2 = str(num1), str(num2)
# if num2 is smaller than num1,
# then reverse their assignments, and apply left-padding
# to the smaller number.
if int(num2) < int(num1):
num1, num2 = num2.zfill(len(num1)), num1
else:
num1, num2 = num1.zfill(len(num2)), num2
carries_list = [0]
new_answer = ''
for i in range(len(num2)):
dig_mul = str(int(num1[-1])*int(num2[len(num2) - i - 1]) + carries_list[i])
if i < len(num2) - 1:
new_answer = dig_mul[-1] + new_answer
carries_list.append(int(dig_mul[:-1].zfill(1)))
else:
new_answer = dig_mul + new_answer
new_answer += '0'*(len(num2)-len(str(int(num1))))
carries_list = [car for car in carries_list if car != 0]
if len(str(int(num1))) == 1:
# If this is the last digit in num1,
# then return the sum of the answer of the previous operation
# and the answer of the current operation, counting
# the addition carries in the process.
# Return the final answer as well as the count
# of multiplication and addition carries.
return count_addition_carries_rec([int(answer), int(new_answer)],
answer=0,
carries=carries+len(carries_list))
else:
# If there are more digits in num1, repeat the operation
# with num1 stripped of its last digit.
return count_multiplication_carries_rec(num1[:-1],
num2,
*count_addition_carries_rec([int(answer), int(new_answer)],
answer=0,
carries=carries+len(carries_list)))
count_multiplication_carries_rec 不像加法那样通用,但很容易修复。您可以创建一次使用 2 个数字调用 count_multiplication_carries_rec 的辅助方法,也可以修改当前实现以使用任意数量的整数。
使用这两种方法的例子:
>>> count_addition_carries_rec([99,99])
(198, 1)
>>> count_addition_carries_rec([17, 17, 17])
(51, 2)
>>> count_multiplication_carries_rec(15,15)
(225, 2)
>>> count_multiplication_carries_rec(657,34)
(223380, 6)
如您所见,这些方法返回加法/乘法运算的结果以及执行该运算时发生的进位数。
