#include using namespace std; #define SHIPNUM 6 #define PORTNUM 3 struct SHIP{ char name; // 船名 int timeArrive; // 到達港口時間 int timeCost[PORTNUM]; // 各港口所需作業時間 int port; // 進入了哪個港口 int timeStart; // 何時開始作業 int timeEnd; // 何時結束作業 int timeWaitStart; // 等候開始作業時間 int timeWait; // 需等待多久 }; struct PORT{ int ship; // 目前作業中的船隻編號 int timeEnd; // 作業中的船隻將在何時結束作業 }; int main(){ int data[][SHIPNUM] = { {5,2,3,5,2,5}, {3,2,4,7,1,4}, {4,6,2,3,3,4}, {3,2,3,2,1,4} }; SHIP shipList[SHIPNUM] = { 0 }; PORT portList[PORTNUM] = { 0 }; // 將資料填入結構 for( int i=0; i shipList[j].timeArrive ){ SHIP temp = shipList[i]; shipList[i] = shipList[j]; shipList[j] = temp; } } } // 已完成作業的船數 int completeNum = 0; // 總等待時間 int totalWaitTime = 0; // 時間從 1 開始推進,直到所有船隻作業完成後結束迴圈 for( int time=1; completeNum= portList[i].timeEnd ){ portList[i].ship = -1; portList[i].timeEnd = INT_MAX; } } // 從尚未完成作業的船隻開始檢查 for( int i=completeNum; i shipList[j].port ){ SHIP temp = shipList[i]; shipList[i] = shipList[j]; shipList[j] = temp; } else if( shipList[i].port == shipList[j].port && shipList[i].timeArrive > shipList[j].timeArrive ){ SHIP temp = shipList[i]; shipList[i] = shipList[j]; shipList[j] = temp; } } } // 列印結果 for( int i=0; i