C语言课程设计之活动安排问题.docVIP

  • 50
  • 0
  • 约1.17千字
  • 约 3页
  • 2018-02-08 发布于河南
  • 举报
C语言课程设计之活动安排问题

题目如下: 设有n个活动的集合E={1,2,…,n},其中每个活动都要求使用同一资源,如演讲会场等,而在同一时间内只有一个活动能使用这一资源。每个活动i都有一个要求使用该资源的起始时间si和一个结束时间fi,且si fi 。要求设计程序,使得安排的活动最多。 输入: 输入只有一个用例,第一行为一正整数n,表示活动个数,接下来n行,每行两个整数,分别表示第i号活动的起始时间和结束时间。 输出: 用一行输出所能安排的最多活动数。 样例输入: 11 5 9 0 6 12 14 8 12 1 4 3 5 5 7 3 8 6 10 8 11 2 13 样例输出: 4 编程思想: 本题就是根据每个活动的结束时间进行从小到大的排序,结束时间在前的当然允许更多的活动在同样的时间内安排。排序并比较后一个的开始时间是不是大于前一个活动的结束时间,是,就加把活动数加1,说明能安排。 代码如下: #include iostream using namespace std; struct node { int s; int e; node * next; }; int main() { // freopen(001.in,r,stdin); node * head=new node; node * p,* cur,*te,*q; int num,i; cur=head; head-next=NULL; int n; cinn; num=1; while (n--) { p=new node; cinp-s; cinp-e; p-next=NULL; if (!head-next) { head-next=p; } else { q=head; cur=q-next; while (cur-e p-e cur-next ) { q=cur; cur=cur-next; } p-next=cur; q-next=p; } } te=cur=head-next; while (cur-next) { if (cur-next-s = te-e) { te=cur-next; num++; } // coutcur-e ; cur=cur-next; } coutnumendl; return 0; } 完毕。

文档评论(0)

1亿VIP精品文档

相关文档