磁盘驱动调度问题
题目:
输入一个请求序列:
98,183,37,122,14,124,65,67
n=8
假设磁头一开始的位置start处于c(c不在序列中的任何位置),例如c=53
如果采用最短寻道优先SSTF,输出序列的调度顺序和磁头移动总数。
解题思路:
将请求的序列有序地进行排列,甚至将起始结点 53 也加入序列中进行排列。当前起始结点只需要和左右两边的两个数字进行比较,计算起始点与左右两个结点相差的距离,选择拥有最短距离的结点,并将当前结点作为起始结点。每一步都是选择当前最优解,符合贪心法的性质。
#include<stdio.h>
#include<stdlib.h>
#include<list>
#define MAX 20
using namespace std;
int n=8;
int start = 53;
int Input[] = {
98,183,37,122,14,124,65,67};
list<int>lst;
int sum=0;
struct NodeType{
int num;
list<int>::iterator position;
list<int>best;
};
list<int>::iterator shortest; //暂时存放当前选择的最短结点
void findShort(