寻找三位数

问题描述
  将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数构成
  1:2:3的比例,试求出所有满足条件的三个三位数。
  例如:三个三位数192,384,576满足以上条件。
输入格式
  无输入文件
输出格式
  输出每行有三个数,为满足题设三位数。各行为满足要求的不同解。
import java.util.Scanner;


public class Main {

    /**
     * @param args
     */
    private static int[] num;
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        num = new int[9];
        in.close();
        f(0);
    }
 
    private static void f(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;
                f(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;
    }

}

 

posted @ 2020-02-28 17:08  智阿广  阅读(215)  评论(0)    收藏  举报