《编程之美——微软技术面试心得》“中国象棋的将帅问题”C#实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Class1 {

static byte counter = 81;

static void Main(string[] a) {
while (counter-- != 0) {
if (counter / 9 % 3 == counter % 9 % 3)
continue;
else {
System.Console.WriteLine("a=" + (counter / 9 + 1).ToString() + "b=" + (counter % 9 + 1).ToString());
}
}

System.Console.Read();
}

}

适用于1.8GHZ左右的CPU 50%占用率

1
2
3
4
5
6
7
8
9
10
11
12
13
14

class ManageCpu {
staticvoid Main(string[] a) {
int st = 0;
while (true) {
st = System.Environment.TickCount;

while (System.Environment.TickCount - st < 90) {
}

System.Threading.Thread.Sleep(90);
}
}
}