[Java] 蓝桥杯ADV-83 算法提高 寻找三位数

版权声明:【https://github.com/liuchuo】大四在校生,水平有限,还望学长们多多包涵,Github真诚求Star~不甚感激!!!(卖萌脸ヾ(=^▽^=)ノ https://blog.csdn.net/liuchuo/article/details/82919291

问题描述
将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数构成
1:2:3的比例,试求出所有满足条件的三个三位数。
例如:三个三位数192,384,576满足以上条件。

输入格式
无输入文件
输出格式
输出每行有三个数,为满足题设三位数。各行为满足要求的不同解。

package adv83;

import java.util.Scanner;

public class Main {

    private static int[] num;
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        num = new int[9];
        in.close();
        dfs(0);
    }

    private static void dfs(int pos) {
        if (pos == 9) {
            int one = num[0] * 100 + num[1] * 10 + num[2];
            int two = num[3] * 100 + num[4] * 10 + num[5];
            int three = num[6] * 100 + num[7] * 10 + num[8];
            
            if (one * 2 == two && one * 3 == three) {
                System.out.println(one + " " + two + " " + three);
            }
            
            return;
        }
        
        for (int i = 1; i <= 9; i++) {
            if (isNotSelect(pos, i)) {
                num[pos] = i;
                dfs(pos + 1);
            }
        }
    }
    
    private static boolean isNotSelect(int pos, int value) {
        for (int i = 0; i < pos; i++) {
            if (num[i] == value) {
                return false;
            }
        }
        return true;
    }

}

猜你喜欢

转载自blog.csdn.net/liuchuo/article/details/82919291