分析出偶数必胜的结论不难,但是怎么找出异或关系的,看来真得考验一下真功夫了,不看答案真想不出来。 实现了一下书上的算法,代码如下: #include <iostream> int a[] = {3, 4, 6}; using namespace std; int nim(int *p, int N) { for (int i = 0; i < N; i++) { for (int j = p[i] - 1; j > 0; j--) { int result = 0; // Save current value int temp = p[i]; // Set stone number after fetching p[i] = j; for (int m = 0; m < N; m++) { result = result ^ p[m]; if (result == 0) { for (int m = 0; m < N; m++) { cout << p[m] << " "; } cout << endl; return 0; } } // Roll back p[i] = temp; } } return -1; } int main() { int size = sizeof(a)/sizeof(int); if (nim(a, size) == -1) { cout << "Can not find way to success :(" << endl; } getchar(); return 0; } 扩展问题: 如果规定相反,好像是奇数时先拿的必输,偶数时先拿的必赢。