P1618 三连击(升级版)

题目传送门

一、暴力大模拟

#include <bits/stdc++.h>

using namespace std;

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    bool found = false;
    for (int i1 = 1; i1 <= 9; i1++)
        for (int i2 = 1; i2 <= 9; i2++) {
            if (i1 == i2) continue;
            for (int i3 = 1; i3 <= 9; i3++) {
                if (i1 == i3 || i3 == i2) continue;
                for (int i4 = 1; i4 <= 9; i4++) {
                    if (i4 == i1 || i4 == i2 || i4 == i3) continue;
                    for (int i5 = 1; i5 <= 9; i5++) {
                        if (i5 == i1 || i5 == i2 || i5 == i3 || i5 == i4) continue;
                        for (int i6 = 1; i6 <= 9; i6++) {
                            if (i6 == i1 || i6 == i2 || i6 == i3 || i6 == i4 || i6 == i5) continue;
                            for (int i7 = 1; i7 <= 9; i7++) {
                                if (i7 == i1 || i7 == i2 || i7 == i3 || i7 == i4 || i7 == i5 || i7 == i6) continue;
                                for (int i8 = 1; i8 <= 9; i8++) {
                                    if (i8 == i1 || i8 == i2 || i8 == i3 || i8 == i4 || i8 == i5 || i8 == i6 ||
                                        i8 == i7)
                                        continue;
                                    for (int i9 = 1; i9 <= 9; i9++) {
                                        if (i9 == i1 || i9 == i2 || i9 == i3 || i9 == i4 || i9 == i5 || i9 == i6 ||
                                            i9 == i7 || i9 == i8)
                                            continue;

                                        int a1 = i1 * 100 + i2 * 10 + i3;
                                        int b1 = i4 * 100 + i5 * 10 + i6;
                                        int c1 = i7 * 100 + i8 * 10 + i9;

                                        if (a1 / a == b1 / b && b1 / b == c1 / c && a1 % a == 0 && b1 % b == 0 &&
                                            c1 % c == 0){
                                            cout << a1 << " " << b1 << " " << c1 << endl;
                                            found = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    if (!found)cout << "No!!!";
    return 0;
}

二、STL全排列

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
int a[10];
/**
 总结:STL中的全排列函数框架
  do {

    } while (next_permutation(a + 1, a + 10));

    1、只能配合数组使用
    2、注意范围是前闭后开。
    3、效果是可以所有可能出现的全排列
 */
int main() {
    LL A, B, C, x, y, z, cnt = 0;
    cin >> A >> B >> C;
    for (int i = 1; i <= 9; i++) a[i] = i;
    do {
        x = a[1] * 100 + a[2] * 10 + a[3];
        y = a[4] * 100 + a[5] * 10 + a[6];
        z = a[7] * 100 + a[8] * 10 + a[9];
        if (x * B == y * A && y * C == z * B) //避免除法,防止爆long long
            printf("%lld %lld %lld\n", x, y, z), cnt++;
    } while (next_permutation(a + 1, a + 10));

    if (!cnt)printf("No!!!");
    return 0;
}
原文地址:https://www.cnblogs.com/littlehb/p/15593680.html