給定兩個整型數組,本題要求找出不是兩者共有的元素。
輸入格式:
輸入分別在兩行中給出兩個整型數組,每行先給出正整數N(≤20),隨后是N個整數,其間以空格分隔。
輸出格式:
在一行中按照數字給出的順序輸出不是兩數組共有的元素,數字間以空格分隔,但行末不得有多余的空格。題目保證至少存在一個這樣的數字。同一數字不重復輸出。
代碼如下:
#!/usr/bin/python # -*- coding: utf-8 -*- x = list(map(int,input().split(" "))) y = list(map(int,input().split(" "))) n1 = x[0] n2 = y[0] a = x[1:] b = y[1:] c = a+b d = sorted(set(c),key=c.index) for i in range(0,len(d)): if d[i] not in a or d[i] not in b: if i != len(d)-1: print(d[i],end=" ") else: print(d[i]) # 10 3 -5 2 8 0 3 5 -15 9 100 # 11 6 4 8 2 6 -5 9 0 100 8 1
重點:去重,排序。set去重,sorted排序。指定key=c.index
然后就是循環判斷了。整體來說程序不難,就我寫的這個,大家應該都很好理解。
讀書和健身總有一個在路上