27 . P1618 三連擊(升級版)

題解:這道題是洛谷的第27道題目,我是感覺不需要遞歸全排列,依舊是暴力即可。

源代碼:

#include <iostream>
#include <stdlib.h>
#include <math.h>

using namespace std;

#include <iostream>
#include <stdlib.h>

using namespace std;

bool check(int *first, int *second)
{
    if (first[0] != second[0] && first[0] != second[1] && first[0] != second[2] &&
        first[1] != second[0] && first[1] != second[1] && first[1] != second[2] &&
        first[2] != second[0] && first[2] != second[1] && first[2] != second[2] &&
        first[0] != first[1] && first[0] != first[2] && first[1] != first[2] &&
        second[0] != second[1] && second[0] != second[2] && second[1] != second[2] &&
        first[1] != 0 && first[2] != 0 && second[1] != 0 && second[2] != 0)
        return true;
    else
        return false;
}

int main()
{
    bool is = false;
    int a1, a2, a3;
    cin >> a1 >> a2 >> a3;
    for (size_t i = 100; i < 1000; i++)
    {
        int a = i;
        int b = (a2*a / a1);
        int c = (a3*a / a1);

        int d = a;
        int e = b;
        int f = c;

        if (b < 1000 && c < 1000)
        {
            // 提取位數;
            int aArray[3],
                bArray[3],
                cArray[3];

            for (size_t i = 0; i < 3; i++)
            {
                aArray[i] = a % 10; a /= 10;
                bArray[i] = b % 10; b /= 10;
                cArray[i] = c % 10; c /= 10;
            }

            if (check(aArray, bArray) && check(aArray, cArray) && check(bArray, cArray))
            {
                cout << d << " " << e << " " << f << endl;
                is = true;
            }
        }
    }

    if (!is) cout << "No!!!" << endl;
    system("pause");
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章