洛谷 P1618 三连击(升级版)

别忘无解时,输出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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Ocean__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值