Java implements the minimum number of pta groups

The given numbers 0-9 are several. You can arrange the numbers in any order, but they must all be used. The goal is to make the final number as small as possible (note that 0 cannot be the first place). For example: given two 0s, two 1, three 5, and one 8, the smallest number we get is 10015558.

Now given a number, please write a program to output the smallest number that can be composed.

Input format: The
input gives 10 non-negative integers in a row, the order indicates that we have the number 0, number 1, ... number 9. The integers are separated by a space. The total number of 10 digits does not exceed 50, and it has at least 1 non-zero digit.

Output format:
output the smallest number that can be composed in one line.

Input example:
2 2 0 0 0 3 0 0 1 0

Sample output:
10015558

Problem-solving ideas: In fact, it is easier to think about this problem. The specific idea is to first build an array of all the numbers from 0 to 9, and then use a for loop to traverse from i = 1, if this subscript If the corresponding array value is not less than 0, it is output, which just guarantees that the first output is the smallest except for 0. After that, remember to reduce the value of the array and break it again and again.

import java.util.Scanner;

/**
 *
 * @author lenovo
 */
public class JavaApplication12 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner(System.in);
        
        int[] num = {0,1,2,3,4,5,6,7,8,9};
        
        for(int i = 0; i < 10; i++)
        {
            num[i] = input.nextInt(); //记录各个数出现的个数
        }
        for(int i = 1; i < 10; i++) //整个循环的作用是输出一个比0大的最小数
        {
            if(num[i] != 0)
            {   System.out.print(i);
                num[i]--;
                break;
            }
        }
        for(int i = 0; i < 10; i++) //输出数组全部的值(除上个循环已经输出的)
            for(int j = 0 ; j < num[i]; j++)
                System.out.print(i);
    }
}

If you don't see the question clearly, you may think that the input numbers are the smallest combination, so be sure to see the question clearly and hope it will be helpful to everyone

Published 3 original articles · praised 3 · visits 27

Guess you like

Origin blog.csdn.net/weixin_45834063/article/details/105453787