读入两个整数a和b,输出绝对值a和绝对值b的各对应位乘积之和,如a=1234,b=608,则输出值为:“1×0+2×6+3×0+4×8“的值,即44。
输入格式:
在一行中输入两个数
输出格式:
在一行中输出对应位乘积之和
实现
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
#Need two list type data whose length are equal.
#return int type
def listSum(a,b):
i=0
result=0;
#Transforming element type to int.
a=list(map(lambda x:int(x),a))
b=list(map(lambda x:int(x),b))
while i<len(a):
result+=a[i]*b[i]
i+=1
return result
#Need two string number
#return int type
def cal(a,b):
a=list(a)
b=list(b)
#Deleting the char of '-' to become absolute value.
if a.__contains__('-'):
a.remove('-')
if b.__contains__('-'):
b.remove('-')
aLen=len(a)
bLen=len(b)
return listSum(a[-bLen:],b) if aLen>bLen else listSum(b[-aLen:],a)
def main():
#Maybe raise a error,because there isn't a check.
s=input().split()
print(cal(s[0],s[1]))
#-------
main()
输出
1234 608
44
Q.E.D.