C++经典编程题目(九)玩火柴棍游戏

四人玩火柴棍游戏,每一次都是三个人赢,一个人输。输的人要按赢者手中的火柴
数进行赔偿,即赢者手中有多少根火柴棍,输者就赔偿多少根。现知道玩过四次后,
每人恰好输过一次, 而且每人手中都正好有16根火柴。问此四人做游戏前手中各有
多少根火柴? 编程解决此问题。
#include
using namespace std;

/*
1   2    3    4
16  16  16   16  :now
8   8    8   40  :4 lose
4   4   36   20  :3 lose
2   34  18   10  :2 lose
32  17   9    5  :1 lose


1/2 Backstepping
*/

int main()
{
	int a[4] = {16,16,16,16};

	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			if (j!=i)
			{
				a[j] = a[j] / 2;
				a[i] = a[i] + a[j];
			}
		}
	}

	cout << "In the begining:" << endl;
	for (int i = 0; i < 4; i++)
	{
		cout << (char)(65 + i) << " has " << a[i] <<
			" matches, and lost at round " << 4 - i << endl;
	}


	system("pause");
	return 0;
}

你可能感兴趣的:(C++学习笔记)