美文网首页
贪心算法会场安排问题

贪心算法会场安排问题

作者: Super_邓帅 | 来源:发表于2016-12-31 18:47 被阅读0次

    会场安排问题

    假设要在足够多的会场里安排一批活动,并希望使用尽可能少的会场。设计一个算法进行安排。

    *测试:
    5(活动数目)
    1 23 (1是开始时间,23是结束时间)
    12 28
    25 35
    27 80
    36 50
    输出:
    3 *


    代码:
    #include<stdio.h>
    #define n 5
    
    int main(){
        int p[n][2]={1,23,12,28,25,35,27,80,36,50};
        int t;//记录最后一个*正在进行*的活动 
        int count=0;//记录用了几个会场
        
        t=0;//第一个活动进行,且为最后一个
        count++; 
        
        for(int i=1;i<n;i++){
            if(p[t][1]>p[i][0]){
                count++;
            }else{
                t++;
            }
        }
        
        printf("%d\n",count);
        return 0;
    } 
    
    运行截图

    相关文章

      网友评论

          本文标题:贪心算法会场安排问题

          本文链接:https://www.haomeiwen.com/subject/zhfbvttx.html