题目:用1,2,3,…,9组成3个数abc,def和ghi,每个数字恰好用一次,要求abc:def:ghi=1:2:3。按照"abc def ghi"的格式输出所有解,每行一个解
下面是我的解答代码,但是遗漏了一个输出 267 534 801
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
using namespace std;
int main() {
auto i = 0, j = i, k = j;
for(i=1;i<=9;i++)
for (j = 1; j <= 9; j++) {
for (k = 1; k <= 9; k++) {
auto abc = i * 100 + j * 10 + k, def = 2 * abc, ghi = 3 * abc;
if (ghi >= 1000)continue;
bool g[10]{ false };
g[i] = true; g[j] = true; g[k] = true;
g[def % 10] = true; g[def % 100 / 10] = true; g[def / 100] = true;
g[ghi % 10] = true; g[ghi % 100 / 10] = true; g[ghi / 100] = true;
auto flag = true;
for (int q = 1; q <= 9; q++) {
if (!g[q]) { flag = false;
break;
}
}
if (flag)printf("%d %d %d\n", abc, def, ghi);
}
}
}
#include <cstdio>
int main() {
for (int a = 1; a <= 9; a++) {
for (int b = 1; b <= 9; b++) {
if (b == a) continue;
for (int c = 1; c <= 9; c++) {
if (c == a || c == b) continue;
int abc = a * 100 + b * 10 + c;
int def = abc * 2;
int ghi = abc * 3;
if (ghi > 987) break; // ghi 最大只能是 987
int d = def / 100, e = def / 10 % 10, f = def % 10;
int g = ghi / 100, h = ghi / 10 % 10, i = ghi % 10;
if (d != e && d != f && d != g && d != h && d != i &&
e != f && e != g && e != h && e != i &&
f != g && f != h && f != i &&
g != h && g != i && h != i) {
printf("%d %d %d\n", abc, def, ghi);
}
}
}
}
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话: