ZOJ 1003

显然易见,该问题应该使用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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值