对于两个电梯的调度算法,能否给我讲讲怎么实现呢.
谢谢大家啦!
//电梯调度算法当前移动臂由外向里扫描
#include<iostream.h>
#include<stdio.h>
#define N 100
typedef struct{
int s;
int p;
}link;
int position1(link a[],int n,int va)
{
int t,i=0,min=10000;
while(i<n)
{
if(a[i].s>va&&a[i].p==0&&a[i].s-va<min)
{
min=a[i].s-va;
t=i;
}
i++;
}
return(t);
}
int position2(link a[],int n,int va)
{
int t,i=0,max=1000;
while(i<n)
{
if(a[i].s<va&&a[i].p==0&&va-a[i].s<max)
{
max=va-a[i].s;
t=i;
}
i++;
}
return(t);
}
int is_max(link a[],int n,int va)
{
int i=0;
while(i<n)
{
if(a[i].s>va)
return(0);
i++;
}
return(1);
}
int left(link a[],int n,int f)//
{
int t,i=1,sum=0,flag=1;
while(i<=n)
{
if(flag&&is_max(a,n,f)==0)//往上找
{
t=position1(a,n,f);
sum=sum+a[t].s-f;
i++;
}
else//往下找
{
t=position2(a,n,f);
sum=sum+f-a[t].s;
i++;
flag=0;
}
f=a[t].s;
a[t].p=1;
}
return(sum);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen ("left.txt","r",stdin);
#endif
link data[N];
int i,n,f;
scanf("%d%d",&n,&f);
for(i=0;i<n;i++)
{
scanf("%d",&data[i].s);
data[i].p=0;
}
printf("%d\n",left(data,n,f));
return(0);
}
#include<iostream.h>
#include<stdio.h>
#define N 100
void sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
int left(int a[],int n,int f,int temp)
{
int sum=0,i=0,t;
while(i<n&&a[i]<f)
{
i++;
}
t=i-1;
if(temp==1)
{
while(i<n)
{
printf("%d ",a[i]);
sum=sum+a[i]-f;
f=a[i];
i++;
}
while(t>=0)
{
printf("%d ",a[t]);
sum=sum+f-a[t];
f=a[t];
t--;
}
}
else
{
while(t>=0)
{
printf("%d ",a[t]);
sum=sum+f-a[t];
f=a[t];
t--;
}
while(i<n)
{
printf("%d ",a[i]);
sum=sum+a[i]-f;
f=a[i];
i++;
}
}
printf("\n");
return(sum);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen ("left.txt","r",stdin);
#endif
int i,n,f,a[N];
scanf("%d%d",&n,&f);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,n);
printf("From out to in:%d\n",left(a,n,f,1));
printf("From in to out:%d\n",left(a,n,f,0));
return(0);
}