《算法竞赛入门经典》习题2-6

Description

用1,2,3……,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要求abc:def:ghi=1:2:3.输出所有解。
AC代码

#include <iostream>
using namespace std;
bool _sum(int abc,int def,int ghi)
{
    bool p[10];
    for(bool *begin =p + 1,*end = p + 10;begin != end;++begin)
        *begin = false;
    for(int i = 0;i < 3;++i)
    {
        p[abc%10] = p[def%10] = p[ghi%10] =true;
        abc /= 10;
        def /= 10;
        ghi /= 10;
    }
    for(bool *begin =p+1,*end = p + 10;begin != end;++begin)
        if(!(*begin))
            return false;
    return true;
}
int main()
{
    for(int i = 123;i < 333;++i)
    {
        int j = i*2, k = i * 3;
        if(_sum(i,j,k))
            cout << i << '	' << j << '	' << k << endl;
    }
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/h-hg/p/8601355.html