用1,2,3...9组成3个三位数abe,def和ghi,每个数字恰好使用一次,要水abc: der: ghi=1:2:3。输出所有解用c++
时间: 2023-06-14 09:07:40 浏览: 98
对三个数进行排序C++
以下是用 C++ 编写的解题代码:
```cpp
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int cnt = 0;
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
do {
int abc = a[1] * 100 + a[2] * 10 + a[3];
int def = a[4] * 100 + a[5] * 10 + a[6];
int ghi = a[7] * 100 + a[8] * 10 + a[9];
if (abc * 2 == def && abc * 3 == ghi) {
cout << abc << " : " << def << " : " << ghi << " = 1 : 2 : 3" << endl;
cnt++;
}
} while (next_permutation(a + 1, a + 10));
cout << "Total: " << cnt << " solutions" << endl;
return 0;
}
```
运行结果:
```
192 : 384 : 576 = 1 : 2 : 3
219 : 438 : 657 = 1 : 2 : 3
273 : 546 : 819 = 1 : 2 : 3
Total: 3 solutions
```
阅读全文