“1小于x小于y小于80,把x与y的和告诉甲,x与y的积告诉乙”

/*
1<x<y<80
把x+y告诉甲,x*y告诉乙
甲对乙说:“我不知道这两个数,不过你也不知道”
乙说:“我现在知道了”
甲说:“我现在也知道了”
*/
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

class foo;
typedef vector< foo > foo_set;
typedef vector< foo >::iterator foo_iterator;

class foo
{
public:
    foo(int _x, int _y)
    {
        x = _x;
        y = _y;
        sum = x+y;
        acc = x*y;
    }

    foo_set get_sum_similar() const;
    foo_set get_acc_similar() const;
    bool has_multi_factor_pair() const;

    int x;
    int y;
    int sum;
    int acc;
};

inline ostream& operator<<(ostream& target, const foo& f)
{
    target << f.x << "/t" << f.y << "/t" << f.sum << "/t" << f.acc;
    return target;
}

struct if_remove1
{
    bool operator()(const foo& f);
};

struct if_remove2
{
    bool operator()(const foo& f);
};

struct if_remove3
{
    bool operator()(const foo& f);
};

int main()
{
    foo_set result;
    for (int x = 2; x < 80; ++x)
    {
        for (int y = x+1; y < 80; ++y)
        {
            result.push_back(foo(x, y));
        }
    }

    /*
    甲猜乙不知道,则,甲的相同和集合中元素可多重分解
    */
    result.erase(remove_if(result.begin(), result.end(), if_remove1()), result.end());

    /*
    乙知道,则,乙的相等积集合中元素有且仅有一个满足上述“甲的观点”
    */
    result.erase(remove_if(result.begin(), result.end(), if_remove2()), result.end());

    /*
    甲知道,则,甲的相等和集合中元素有且仅有一个满足上述“乙的观点”
    */
    result.erase(remove_if(result.begin(), result.end(), if_remove3()), result.end());

    copy(result.begin(), result.end(), ostream_iterator< foo >(cout, "/n"));

 return 0;
}

foo_set foo::get_sum_similar() const
{
    foo_set fs;
    for (int i = 2; i < sum/2; ++i)
    {
        fs.push_back(foo(i, sum-i));
    }
    return fs;
}

foo_set foo::get_acc_similar() const
{
    foo_set fs;
    for (int i = 2; i < ::sqrt(acc); ++i)
    {
        if (acc % i == 0 && acc/i < 80)
        {
            fs.push_back(foo(i, acc/i));
        }
    }
    return fs;
}

bool foo::has_multi_factor_pair() const
{
    int times = 0;
    for (int _x = 2; _x < ::sqrt(acc); ++_x)
    {
        if (acc % _x == 0)
        {
            ++times;
        }

        if (times > 1)
        {
            return true;
        }
    }
    return false;
}

bool if_remove1::operator()(const foo& f)
{
    foo_set fs = f.get_sum_similar();
    for (foo_iterator it = fs.begin(); it != fs.end(); ++it)
    {
        if (!it->has_multi_factor_pair())
        {
            return true;
        }
    }
    return false;
}

bool if_remove2::operator()(const foo& f)
{
    int times = 0;
    foo_set fs = f.get_acc_similar();
    for (foo_iterator it = fs.begin(); it != fs.end(); ++it)
    {
        if (!if_remove1()(*it))
        {
            ++times;
        }

        if (times > 1)
        {
            return true;
        }
    }
    if (times == 1)
    {
        return false;
    }
    return true;
}

bool if_remove3::operator()(const foo& f)
{
    int times = 0;
    foo_set fs = f.get_sum_similar();
    for (foo_iterator it = fs.begin(); it != fs.end(); ++it)
    {
        if (!if_remove2()(*it))
        {
            ++times;
        }

        if (times > 1)
        {
            return true;
        }
    }
    if (times == 1)
    {
        return false;
    }
    return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值