给一些活动,要求分配会场。
#include<stdio.h>
#include<stdlib.h>
int greedySelector(int *s,int *f,int len)
{
int n = len - 1;
int count = 0;
int j = 0;
int flag = 1;
int num = 0;
//for(int i=0;i<len-1;i++) //判断时间是否正确
//{
// if((s[i]>=f[i])||(s[i]<0)||(f[i]<0))
// {
// printf("事件%d输入有误,请重新输入\n",i+1);
// printf("开始时间:");
// scanf("%d",&s[i]);
// printf("结束时间:");
// scanf("%d",&f[i]);
//
// }
//
// i--;
//}
int tmp1; //保证事件结束时间非减序
for(int i=0;i<n;i++)
{
for(int k=0;k<n-i;k++)
{
if(f[k]>f[k+1])
{
tmp1 = f[k];
f[k] = f[k+1];
f[k+1] = tmp1;
tmp1 = s[k];
s[k] = s[k+1];
s[k+1] = tmp1;
}
}
}
int *tmp = (int *)malloc(n*sizeof(int));//给标记数组初始化
for(int i=0;i<=n;i++)
{
tmp[i] = 0;
}
tmp[0] = 1;
printf("事件:%d 开始:%d 结束:%d\n",1,s[0],f[0]);
while(count<n)
{
for(int i=0;i<=n;i++)
{
if(tmp[i] == 0)
{
if(flag==0)
{
flag = 1;
j = i;
tmp[j] = 1;
printf("事件:%d 开始:%d 结束:%d\n",i+1,s[i],f[i]);
count++;
}
if(s[i]>=f[j])
{
j = i;
tmp[i] = 1;
printf("事件:%d 开始:%d 结束:%d\n",i+1,s[i],f[j]);
count++;
}
}
}
num++;
printf("~~~~~~~~~~《会场:%d》~~~~~~~~~~~~~~\n",num);
flag = 0;
}
return num;
}
int main()
{
///*int srr[] = {3,1};
//int frr[] = {5,3};*/
//int srr[] = {1,3,0,5,3,5,6,8,8,2,12};//test
//int frr[] = {4,5,6,7,8,9,10,11,12,13,14};
printf("~~~~~~~~~MENU~~~~~~~~~~\n");
printf(" 1.会场安排\n");
printf(" 2.退出\n");
int eg1;
scanf("%d",&eg1);
if(eg1 != 1)
{
return 0;
}
int events; //输入数据
int flag1 = 1;
printf("事件总个数:");
scanf("%d",&events);
int *srr = (int*)malloc(events*sizeof(int));
int *frr = (int*)malloc(events*sizeof(int));
for(int i=0;i<events;i++)
{
printf("事件%d",i+1);
printf("事件%d\n",i+1);
printf("开始时间:");
scanf("%d",&srr[i]);
printf("结束时间:");
scanf("%d",&frr[i]);
begin:if((srr[i]>=frr[i])||(srr[i]<0)||(frr[i]<0))
{
printf("事件%d输入有误,请重新输入\n",i+1);
printf("开始时间:");
scanf("%d",&srr[i]);
printf("结束时间:");
scanf("%d",&frr[i]);
goto begin;
}
}
printf("共%d个会场\n",greedySelector(srr,frr,events));
free(srr);
free(frr);
return 0;
}