c++算法入门题 9

 1 /*
 2 9. 四人玩火柴棍游戏,每一次都是三个人赢,一个人输。输的人要按赢者手中的火柴
 3   数进行赔偿,即赢者手中有多少根火柴棍,输者就赔偿多少根。现知道玩过四次后,
 4   每人恰好输过一次, 而且每人手中都正好有16根火柴。问此四人做游戏前手中各有
 5   多少根火柴? 编程解决此问题。
 6   */
 7 
 8 #include <iostream>
 9 
10 using namespace std;
11 int FourPerson[4];
12 void computeEach(int FourPerson[4], int index);
13 void main(void)
14 {
15     for (int i = 0; i < 4; i++//
16     {
17         FourPerson[i] = 16;
18     }
19     
20     for (i = 3; i >= 0 ; i--//
21     {
22         computeEach(FourPerson, i);
23     }
24     
25     cout << "In the beginning each person's number is:" << '\n';
26     for (i = 0; i < 4; i++)
27     {
28         cout << FourPerson[i];
29         if ( i < 3)
30         {
31             cout << "";
32         } else 
33         {
34             cout << '\n';
35         }
36         
37     }
38 }
39 
40 void computeEach(int FourPerson[4], int index)
41 {
42     int sumOfTheWinner = 0;
43     for (int i = 0; i < 4; i++)
44     {
45         if (i == index)
46         {
47             continue;
48         }
49         FourPerson[i] = FourPerson[i] / 2;
50         sumOfTheWinner += FourPerson[i];
51     }
52     FourPerson[index] = 64 - sumOfTheWinner;
53 }
54 

posted on 2009-04-12 22:33 walking snail 阅读(57) 评论(0)  编辑 收藏 引用

导航

<2025年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿

随笔分类

文章分类(13)

文章档案(16)

相册

搜索

最新评论