欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

1002 A+B for Polynomials (25 point(s))

程序员文章站 2022-03-30 11:25:57
if __name__=='__main__': a=input().split() a=[x for x in a] b=input().split() b=[x for x in b] # a=[2,1,2.4,7,3.2] # b=[2,2,1.5,1,0.5] num_a=int(a[0]) num_b=int(b[0]) a=a[1:] b=b[1:] result={} for i in rang.....

1002 A+B for Polynomials (25 point(s))


if __name__=='__main__':
    a=input().split()
    a=[x for x in a]
    b=input().split()
    b=[x for x in b]
    # a=[2,1,2.4,7,3.2]
    # b=[2,2,1.5,1,0.5]
    num_a=int(a[0])
    num_b=int(b[0])
    a=a[1:]
    b=b[1:]
    result={}
    for i in range(0,num_a*2,2):
        # dict中填入数据
        result.update({a[i]:a[i+1]})
    for j in range(0,num_b*2,2):
        # 对result中的数据进行检索
        found=set(result)

        found=list(found)

        if found.count(b[j])==0:
            result.update({b[j]:b[j+1]})
        else:
            result.update({b[j]:float(result.get(b[j]))+float(b[j+1])})
    keys = list(sorted(result.items(), key=lambda d:d[0], reverse = True))
    count=len(keys)
    # print(count)
    # print(keys)
    # tuple转换为list
    come=[]
    come.append(count)
    for w in keys:
        come.append(list(w)[0])
        come.append(list(w)[1])
    # print(come)
    for x, c in enumerate(come):
        print('{}'.format(come[x]), end=' ' if int(x) != len(come) - 1 else '')

# NO extra space at the end of each line

 

本文地址:https://blog.csdn.net/lshenbiandedian/article/details/113976101

相关标签: python