编程之美——楼梯调度算法
#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);
}
本是菜鸟,偶做老鸟,略读半卷书,坐井说天阔。大志无所为,海斗量得失,年到老时方恨晚,怒指生不逢时。