
TQC+ 程式語言Python 705 子集合與超集合
題目說明:
請撰寫一程式,依序輸入五個、三個、九個整數,並各自儲存到集合set1、set2、set3中。接著回答:set2是否為set1的子集合(subset)?set3是否為set1的超集合(superset)?
輸入與輸出會交雜如下,輸出的部份以粗體字表示
Input to set1:
3
28
-2
7
39
Input to set2:
2
77
0
Input to set3:
3
28
12
99
39
7
-1
-2
65
set2 is subset of set1: False
set3 is superset of set1: True
3
28
-2
7
39
Input to set2:
2
77
0
Input to set3:
3
28
12
99
39
7
-1
-2
65
set2 is subset of set1: False
set3 is superset of set1: True
程式碼:
set1 = set()
set2 = set()
set3 = set()
print("Input to set1:")
for i in range(5):
set1.add(eval(input()))
print("Input to set2:")
for j in range(3):
set2.add(eval(input()))
print("Input to set3:")
for k in range(9):
set3.add(eval(input()))
print('set2 is subset of set1:', set2.issubset(set1))
print('set3 is superset of set1:', set3.issuperset(set1))
TQC+ 程式語言Python 第7類:數組(Tuple)、集合(Set)以及詞典(Dictionary)
如果覺得文章內容還不錯的話,麻煩請幫我點個讚!感謝
可以多點幾次喔~~
第一次點讚需使用 Google 或 Facebook 帳號註冊


一個評論
自動引用通知: