#include <stdio.h>
#define MAXLENGTH 10 //最大作业数量
int d[MAXLENGTH]; //机器空闲时间
int S[MAXLENGTH][MAXLENGTH]; //每台机器的处理时间
struct work
{ //定义结构体,记录作业处理时间
int hour; //作业时间
int number; //源顺序
};
work t[MAXLENGTH];
//数组排序
void bubble_sort(work a[], int n)
{
int i, j;
work temp;
for (int j = 0; j < n - 1; j++)
{
for (int i = 0; i < n - 1 - j; i++)
{
if (a[i].hour < a[i + 1].hour)
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
else if (a[i].hour == a[i + 1].hour && a[i].number > a[i + 1].number)
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
}
//作业个数10 时间{41,67,43,10,69,24,78,58,62,64} 机器个数 5
//多机调度声明
void MultiMachine(work t[], int n, int d[], int m);
int main()
{
int n; //作业个数
int m; //机器个数
int i; //用于计数
printf("请输入待处理的作业个数:");
scanf("%d", &n);
printf("请输入作业需要处理的时间:");
for (i = 0; i < n; i++)
{
scanf("%d", &t[i].hour);
t[i].number = i + 1;
}
//对结构体数组进行从小到大的排序,序号存放在number中
bubble_sort(t, n);
printf("请输入机器个数:");
scanf("%d", &m);
for (int i = 0; i < m; i++)
{
d[i] = 0;
}
MultiMachine(t, n, d, m);
return 0;
}
void MultiMachine(work t[], int n, int d[], int m)
{
//队尾下标
int rear[MAXLENGTH];
int i, j, k;
//安排前m个作业
for (i = 0; i < m; i++)
{
S[i][0] = t[i].number;
rear[i] = 0;
d[i] = t[i].hour;
}
//安排余下几个作业
for (i = m; i < n; i++)
{
//查找最先空闲的机器
for (j = 0, k = 1; k < m; k++)
{
if (d[k] < d[j])
{
j = k;
}
}
rear[j]++;
S[j][rear[j]] = t[i].number;
d[j] += t[i].hour;
}
//输出结果
for (i = 0; i < m; i++)
{
printf("机器%d处理:", i + 1);
for (j = 0; S[i][j] > 0; j++)
{
printf("作业%d\t", S[i][j]);
}
printf("处理时间:%d\n", d[i]);
}
//找到最短处理时间
int shortest_time = d[0];
for (i = 1; i < m; i++)
{
if (d[i] > shortest_time)
{
shortest_time = d[i];
}
}
printf("最短处理时间:%d\n", shortest_time);
}
多机调度问题
最新推荐文章于 2024-09-06 10:38:06 发布