ZOJ/ZJU 1003

 

  题意:给出两个数,如果在1-100之间能找到若干个数的乘积是a,还能找到若干个数的乘积是b,而且没有交集。那max(a,b)输出,如果都找不到输出max(a,b)。如果小的能找到大的找不到输出min(a,b)。

  解法:搜索。其实这个题我之前一直不会,然后搜了下题解,发现简单的搜索就能过。只要能找到一种组成a,b的情况那么就是大的赢。否则看是不是能找到一种组成小数的情况,那就是小的赢。结果就是判断小的赢还是不赢。如果能想到这也就简单了吧。

 

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <string.h>
#include <cstdio>

using namespace std;

int win;

void solve(int a, int b, int s) {
    if ( win == 1 ) return;
    if ( a == 1 && b == 1 ) {
        win = 1;
        return;
    }
    if ( s == 101 ) {
        if ( a == 1 && b > 1 )
            win = 0;
        return;
    }
    if ( a % s == 0 ) solve(a / s, b, s + 1);
    if ( b % s == 0 ) solve(a, b / s, s + 1);
    solve(a, b, s + 1);
}


int main() {

    int n, m;

    while (scanf("%d%d", &n, &m) != EOF) {

        win = -1;
        solve(min(n, m), max(n, m), 2);

        if ( win != 0 ) {
            printf("%d\n", max(n, m));
        }

        else {
            printf("%d\n", min(n, m));
        }
    }
    return 0;
}
posted @ 2011-09-20 20:33  like@neu  阅读(253)  评论(0编辑  收藏  举报