《编程之美——微软技术面试心得》的C#实现---电梯调度问题解法一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Program
{
static void Main(string[] args)
{
int[] person = {0, 1, 1, 1, 1, 1, 15}; //总共六层楼,没有0层
int needed = 0;
int min = 0;
int target = 0;
for (int i = 1; i <= 6; i++)
{
needed = 0;
for (int j = 1; j < i; j++)
{
//目的在i层以下的
needed += person[j] * (i - j);
}

for (int j = i + 1; j <= 6; j++)
{
//目的在i层上面的
needed += person[j] * (j - i);
}

if (min == 0 || min >= needed)
{
min = needed;
target = i;
}
}

Console.WriteLine(target);
Console.Read();
}