编程之美——楼梯调度算法

#include "stdlib.h"
#include <iostream>
#include<string>
#include<iostream.h>
void b(int nPerson[7])// nPerson[i]表示到第i层的乘客数目
{
int nFloor, nMinFloor, nTargetFloor;
nTargetFloor = -1;
for(int i = 1; i <= 6; i++)
{
nFloor = 0;
int j;
for(j = 1; j < i; j++)
nFloor += nPerson[j] * (i - j);
for(j = i + 1; j <= 6; j++)
nFloor += nPerson[j] *(j - i);
cout<<""<<i<<"层楼停"<<" 总要走的楼层是"<<nFloor<<endl;
if(nTargetFloor == -1 || nMinFloor > nFloor)
{
nMinFloor = nFloor;
nTargetFloor = i;
}
}
cout<<"最优方案是在第"<<nTargetFloor<<"层楼停,只需走"<<nMinFloor<<""<<endl;
}
void main()
{
int a[7];
cout<<"请输入各层楼的人数:";
for(int i=1;i<7;i++)
cin>>a[i];
b(a);
}

 

posted @ 2012-02-27 14:03  Ghost Soar  阅读(452)  评论(0编辑  收藏  举报