#include <bits/stdc++.h>
const int maxn = 1000;
using namespace std;
int num;
struct meeting
{
int b_time;
int e_time;
meeting()
{
b_time = 0;
e_time = 0;
}
};
meeting m[maxn];
queue<meeting> record;
bool cmp(meeting m1, meeting m2)
{
if(m1.e_time == m2.e_time)
return m1.b_time < m2.b_time;
else
return m1.e_time < m2.e_time;
}
int main()
{
cout << "Input the number of the meeting:";
cin >> num;
for(int i = 0 ; i < num; i++){
cin >> m[i].b_time >> m[i].e_time;
}
sort(m,m+num,cmp);
for(int i = 0 ; i < num ; i++){
cout << m[i].b_time << "," << m[i].e_time << endl;
}
int now_e_time = m[0].e_time;
int cnt = 1;
record.push(m[0]);
for(int i = 1 ; i < num ; i++)
{
if(m[i].b_time > now_e_time)
{
now_e_time = m[i].e_time;
cnt++;
record.push(m[i]);
}
}
cout << "Max number of the meeting:" << cnt << endl;
while(!record.empty())
{
meeting head = record.front();
record.pop();
cout << head.b_time << "," << head.e_time << endl;
}
return 0;
}
运行结果:
代码:(Python3.6版本)
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 18 17:26:16 2018
@author: pprp
"""
sav = []
def answer(meetings):
last_end = -1
ans = 0
for end,start in sorted((end,start) for start,end in meetings ):
if start >= last_end:
last_end = end
ans += 1
sav.append([start,end])
print("最佳安排会议个数为: %s" % ans)
print("以下为被安排的会议:")
for a in sav:
print(a)
meetings = [(2,3),(1,5),(4,6),(5,7),(6,9),(9,11)]
answer(meetings)