算法题:巧妙填数

描述

1,2,\cdots,91,2,,999个数分成三组,分别组成三个三位数,且使这三个三位数构成1:2:31:2:3的比例。

试求出所有满足条件的三个三位数。
例如:三个三位数192,384,576192,384,576满足以上条件。

格式

输入格式

无输入。

输出格式

需要输出全部结果。每行输出3个数 用空格隔开。按照字典序的顺序输出。


我的答案:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

class Main
{
    public static void main(String[] argv)
    {
        try {
            char[] arr = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
            for (Integer a = 123; a <= 329; a++) {
                boolean output = true;
                Integer b = a * 2;
                Integer c = a * 3;
                String number = a.toString() + " " + b.toString() + " " + c.toString();
                for (char ch : arr) {
                    if (number.indexOf(ch) == -1) {
                        output = false;
                    }
                }
                //output
                if (output) {
                    System.out.println(number);
                }
            }

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}


猜你喜欢

转载自blog.csdn.net/loophome/article/details/79212430
今日推荐