#includeusing namespace std; #define MAXSIZE 5 //int s[5] = { 1,3,6,8,9 }; //int f[5] = { 2,6,9,11,12 }; void timeSelector(int s[], int f[], int n) { int j = 0, index = 1; int temp[MAXSIZE] = { 1 }; for (int i = 1; i < n; i++) { if (s[i] >= f[j]) { temp[index++] = i + 1; //记录相容活动的下标 j++; } else continue; } //输出最优相容解 cout << "最大相容子集有:" << index << "个" << endl; //输出相容活动的各个时间段 for (int k = 0; k <= j; k++) { cout << "第" << temp[k] << "个活动开始于:" << s[temp[k]-1] << " 结束于:" << f[temp[k]-1] << endl; } } int main() { int s[MAXSIZE], f[MAXSIZE]; cout << "请输入起始时间数组元素:" << endl; for (int i = 0; i < MAXSIZE; i++) { cin >> s[i]; } cout << "请输入结束时间数组元素:" << endl; for (int i = 0; i < MAXSIZE; i++) { cin >> f[i]; } timeSelector(s, f, MAXSIZE); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)