Problem-solving idea : This is a typical greedy algorithm problem, that is, it does not consider the overall but focuses on the local optimal solution.
I have a total of three steps to solve the problem. First, how to enter the timetable into the program, and then to sort the timetable. Here I use the end time as a benchmark to sort from small to large, and finally use the greedy algorithm to traverse in turn to find suitable activities and output.
#include <stdio.h>
main()
{
int arr[2][100];
int n;
int i,j,end,temp;
int k = 0;
int add = 1;
scanf("%d",&n);
//第一步:数据导入
for(i = 0;i < 2;i++){
for(j = 0;j < n;j++){
scanf("%d",&arr[i][j]);
}
}
//第二布:数据排序
for(i = 0;i < n;i++){
temp = arr[1][i];
k = i;
for(j = i;j < n;j++){
if(arr[1][j] < temp)
{
temp = arr[1][j];
k = j;
}
}
temp = arr[0][i];
arr[0][i] = arr[0][k];
arr[0][k] = temp;
temp = arr[1][i];
arr[1][i] = arr[1][k];
arr[1][k] = temp;
}
//第三布:遍历寻找
end = arr[1][0];
printf("第%d个活动被安排:%d开始,%d结束,\n",1,arr[0][1],arr[1][1]);
for(i = 1;i < n;i++){
if(arr[0][i] > end){
printf("第%d个活动被安排:%d开始,%d结束,\n",i+1,arr[0][i],arr[1][i]);
end = arr[1][i];
add++;
}
}
printf("总共%d个活动被安排",add);
}