活動選擇問題的 Python 程式
在本文中,我們將學習如何解決下面給出的問題陳述。
問題陳述−給我們 n 個活動及其各自的開始和結束時間。我們需要選擇最多數量的單人活動,假設他一次只從事一項活動。
變數符號
N - 活動總數
S - 包含所有活動開始時間的陣列
F - 包含所有活動結束時間的陣列
現在讓我們觀察下面實現中的解決方案 −
# 貪婪演算法
例如
# maximum number of activities that can be performed by a single person
def Activities(s, f ):
n = len(f)
print ("The selected activities are:")
# The first activity is always selected
i = 0
print (i,end=" ")
# For rest of the activities
for j in range(n):
# if start time is greator than or equal to that of previous activity
if s[j] >= f[i]:
print (j,end=" ")
i = j
# main
s = [1, 2, 0, 3, 2, 4]
f = [2, 5, 4, 6, 8, 8]
Activities(s, f)輸出
The selected activities are: 0 1

所有變數都在本地範圍內宣告,其引用如上圖所示。
結論
在本文中,我們瞭解瞭如何編寫活動選擇問題的 Python 程式
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP