在已经完成拓扑排序的基础上,完成 允许用户指定下列两种编排策略之一:一是使学生在各学期中的学习负担尽量均匀;二是使课程尽可能地集中在前几个学期中。,并给出我完整的C语言代码
时间: 2024-05-03 20:19:37 浏览: 22
以下是基于已完成拓扑排序的C语言代码,实现用户指定编排策略的功能:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_COURSES 100
int courses[MAX_COURSES][MAX_COURSES]; // 存储课程关系的邻接矩阵
int indegree[MAX_COURSES]; // 存储每个课程的入度
int course_order[MAX_COURSES]; // 存储拓扑排序后的课程顺序
int num_courses; // 课程总数
int max_terms; // 最大学期数
int terms[MAX_COURSES]; // 存储每个课程的安排学期
int strategy; // 用户指定的编排策略,1表示均匀,2表示集中在前几个学期
// 拓扑排序
void topological_sort() {
int queue[MAX_COURSES];
int front = 0, rear = 0;
int i, j;
// 将所有入度为0的课程加入队列
for (i = 0; i < num_courses; i++) {
if (indegree[i] == 0) {
queue[rear++] = i;
}
}
// 循环处理队列中的课程
while (front != rear) {
int course = queue[front++];
course_order[front - 1] = course;
// 更新与当前课程相邻的课程的入度
for (j = 0; j < num_courses; j++) {
if (courses[course][j] == 1) {
indegree[j]--;
if (indegree[j] == 0) {
queue[rear++] = j;
}
}
}
}
}
// 安排学期
void arrange_terms() {
int i, j;
if (strategy == 1) { // 均匀分配
for (i = 0; i < num_courses; i++) {
int min_terms = terms[course_order[i]] = 1;
for (j = 0; j < i; j++) {
if (courses[course_order[j]][course_order[i]] == 1) {
min_terms = (terms[course_order[j]] >= min_terms) ? terms[course_order[j]] + 1 : min_terms;
}
}
terms[course_order[i]] = min_terms;
if (max_terms < min_terms) {
max_terms = min_terms;
}
}
} else if (strategy == 2) { // 集中在前几个学期
for (i = 0; i < num_courses; i++) {
int max_terms_from_pre = 0;
for (j = 0; j < i; j++) {
if (courses[course_order[j]][course_order[i]] == 1) {
max_terms_from_pre = (terms[course_order[j]] > max_terms_from_pre) ? terms[course_order[j]] : max_terms_from_pre;
}
}
terms[course_order[i]] = max_terms_from_pre + 1;
if (max_terms < terms[course_order[i]]) {
max_terms = terms[course_order[i]];
}
}
}
}
int main() {
int i, j;
// 初始化
for (i = 0; i < MAX_COURSES; i++) {
for (j = 0; j < MAX_COURSES; j++) {
courses[i][j] = 0;
}
indegree[i] = 0;
course_order[i] = -1;
terms[i] = 0;
}
// 输入课程总数和先修关系
printf("请输入课程总数:");
scanf("%d", &num_courses);
printf("请输入先修关系,用1表示有先修关系,0表示没有:\n");
for (i = 0; i < num_courses; i++) {
for (j = 0; j < num_courses; j++) {
scanf("%d", &courses[i][j]);
if (courses[i][j] == 1) {
indegree[j]++;
}
}
}
// 输入编排策略
printf("请选择编排策略:\n");
printf("1. 使学生在各学期中的学习负担尽量均匀\n");
printf("2. 使课程尽可能地集中在前几个学期中\n");
scanf("%d", &strategy);
// 拓扑排序并安排学期
topological_sort();
arrange_terms();
// 输出结果
printf("课程拓扑排序后的顺序为:");
for (i = 0; i < num_courses; i++) {
printf("%d ", course_order[i] + 1);
}
printf("\n");
printf("课程安排的最大学期数为:%d\n", max_terms);
printf("各门课程的安排学期为:\n");
for (i = 0; i < num_courses; i++) {
printf("课程%d:第%d学期\n", i + 1, terms[i]);
}
return 0;
}
```
在程序中,我们新增了一个 `strategy` 变量,用于存储用户指定的编排策略(1表示均匀,2表示集中在前几个学期)。根据不同的编排策略,在拓扑排序后,我们使用 `arrange_terms()` 函数对课程进行安排学期。该函数根据用户指定的编排策略,计算出每个课程的最早安排学期,并将结果存储在 `terms` 数组中。
编译运行代码后,可以按照提示输入课程总数、先修关系和编排策略。程序会输出课程拓扑排序后的顺序、课程安排的最大学期数和各门课程的安排学期。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)