活动安排问题C语言

  • Post author:
  • Post category:其他


#include<stdio.h>
void ChoseEvent(int n, int s[], int f[], int Flag[]){
	Flag[1] = true;  //第一个活动一定安排
	int j = 1;    //最近一次加入的活动
	for (int i = 2; i <= n; i++)//检查活动i是否与已选择的活动相容{
		if (s[i] >= f[j]){
			Flag[i] = 1;
			j = i;
		}
		else
			Flag[i] = 0;
	}
}
void print(int n, int s[], int f[], int Flag[]){
	printf("       时间轴     ");
	for (int k = 1; k <= 23; k++){
		printf("%3d", k);
	}
	printf("\n");
	for (int i = 1; i <= n; i++){      //输出被安排的活动编号和它的开始时间和结束时间
		if (Flag[i]){
			printf("举办活动%2d:%2d->%2d  ", i, s[i], f[i]);
			for (int j = 1; j <= f[i]; j++){
				if (j < s[i])
					printf("   ");
				else
					printf("|-|");
				//printf("---");
			}
			printf("\n");
		}
		else{
			printf("舍弃活动%2d:%2d->%2d  ", i, s[i], f[i]);
			for (int j = 1; j <= f[i]; j++){
				if (j < s[i])
					printf("   ");
				else
					printf("| |");
				//printf("- -");	
			}
			printf("\n");
		}
	}
}
int main(){
	int start[] = { 0,1,3,0,5,3,5,6,8,8,2,12,5,6,3,8,7,11,16,9,22 };//活动开始时间
	int finally[] = { 0,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 };//活动结束时间
	int flag[30];//活动是否安排的标志
	int n = 20;
	ChoseEvent(n, start, finally, flag);
	print(n, start, finally, flag);
	return 0;
}

在这里插入图片描述



版权声明:本文为ZSJ27原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。