首页 > 试题广场 >

特殊乘法

[编程题]特殊乘法
  • 热度指数:29841 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
写个算法,对2个小于1000000000的输入,求结果。 特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5

输入描述:
两个小于1000000000的数


输出描述:
输入可能有多组数据,对于每一组数据,输出Input中的两个数按照题目要求的方法进行运算后得到的结果。
示例1

输入

123 45

输出

54
while True:
    try:
        a,b = input().split()
        count = 0
        for i in a:
            for j in b:
                count += int(i)*int(j)
        print(count)
    except:
        break


发表于 2020-12-11 11:45:38 回复(0)
while True:
    try:
        m,n=input().strip().split(' ')
        #print(m,n)
        sum1=0
        m=list(m)
        n=list(n)
        lenth1=len(m)
        lenth2=len(n)
        min_num=min(lenth1,lenth2)
        if lenth1==min_num:
            re=sum(list(map(int,n)))
            for i in m:
                sum1+=int(i)*re
        else:
            re=sum(list(map(int,m)))
            for i in n:
                sum1+=int(i)*re
        print(sum1)
    except:
        break
发表于 2019-07-30 13:29:54 回复(0)

这是笛卡尔积的运算,在python中天然支持,在itertools包中有product方法。
代码如下,测试已通过:

from itertools import product
while True:
    try:
        a, b = input().split()
        res = 0
        for i in product(map(int, list(a)), map(int, list(b))):
            res += i[0] * i[1]
        print(res)
    except:
        break
编辑于 2017-09-08 11:11:46 回复(1)
from itertools import product
try:
    while 1:
        a, b = raw_input().split()
        print sum(map(lambda x:int(x[0]) * int(x[1]), list(product(a,b))))
except:
    pass

发表于 2016-12-23 23:57:52 回复(0)