直接枚举

P1618 三连击(升级版)

yzzxliuchao @ 2017-02-06 22:52:10

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int h[10];
int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    bool found = false;
    for(int i = 1;;i++)  //从小到大枚举a、b、c的i倍数 
    {
        memset(h,0,sizeof(h));
        int ai, bi, ci;
        ai = a * i;
        bi = b * i;
        ci = c * i;
        if(ci > 987) break; //当最大的三位数大于987时停止枚举 
        h[ai % 10] = h[ai / 10 % 10] = h[ai / 100] = 1;     //统计1~9的出现情况 
        h[bi % 10] = h[bi / 10 % 10] = h[bi / 100] = 1;    
        h[ci % 10] = h[ci / 10 % 10] = h[ci / 100] = 1;    
        int sum = 0;
        for(int i = 1; i <= 9; i++)
            sum += h[i];
        if(sum == 9)
        {
            found = true;
            cout << ai << " " << bi << " " << ci << endl;
        }
    }
    if(!found) cout << "No!!!" << endl;
    return 0;    
}

|