显然易见,该问题应该使用DFS解决。
分析:(假定A>B)
如果A要赢,要么A真,要么双假。
如果B要赢,只能B真A假。
故判断结果输出,应判断B能否赢,而不是A能否赢。
其次,气球是唯一的,所以可以从2~100(1不用考虑)正序踩气球。
即dfs(k)->dfs(k+1) k<=100 。
一个气球,要么A踩,要么B踩,要么都不踩,故共3种递归情况。
而有人要踩则必须是其因子,故通过该条件可以提高算法效率。
所以dfs条件如上
代码如下
#include <iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<set>
using namespace std;
bool a ,b;//both false
void dfs(int A, int B, int c) {
if (B == 1) {//b true
b = 1;
if (A == 1)// A true
a = 1;
}
if ((a && b) || c>100)//if both true or c more than 100
return;
if (A%c == 0) {// this is A's
dfs(A / c, B, c + 1);
}
if (B%c == 0) {// this is B's
dfs(A, B / c, c + 1);
}
dfs(A, B, c + 1);// neither
}
int main() {
int A, B;
while (cin >> A >> B) {
if (A < B)
swap(A, B);
a = b = 0;
dfs(A, B, 2);
if (b && !a)
cout << B<<endl;
else cout << A << endl;
}
return 0;
}