别忘无解时,输出No!!!
考察dfs
#include<iostream>
using namespace std;
const int N = 10;
bool st[N];
int A, B ,C;
bool flg = 0;
void dfs(int u,int a, int b, int c) //u表示搜到了第u个数字了
{
if(u >= 7 && a > b) return;//剪枝
if(u >= 10){
if(a * 1.0 / b == A * 1.0 / B && b * 1.0 / c == B * 1.0 / C)
cout << a << " " << b << " " << c << endl, flg = 1;
return;
}
else if(u >= 1 && u <= 3){
for(int i = 1; i <= 9; i ++)
{
if(!st[i])
{
st[i] = true;
dfs(u + 1, a * 10 + i, 0, 0);
st[i] = false;
}
}
}
else if(u >= 4 && u <= 6){
for(int i = 1; i <= 9; i ++)
{
if(!st[i])
{
st[i] = true;
dfs(u + 1, a, b * 10 + i, 0);
st[i] = false;
}
}
}
else if(u >= 7 && u <= 10){
for(int i = 1; i <= 9; i ++)
{
if(!st[i])
{
st[i] = true;
//cout << a << " " << b << " " << c * 10 + i << endl;
dfs(u + 1, a, b , c * 10 + i);
st[i] = false;
}
}
}
}
int main(){
cin >> A >> B >> C;
dfs(1, 0, 0 , 0);
if(flg == 0) cout << "No!!!" << endl;
return 0;
}