PAT B: 1023 minimum number of groups (20 points)

Given a number of numbers 0-9 each. You can arrange these numbers in any order, but you must use them all. 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 5s, and one 8, the smallest number we get is 10015558.

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

Input format:

Input gives 10 non-negative integers in a row, the order indicates that we have the number of numbers 0, number 1, ... number 9. Use a space to separate the integers. The total number of 10 digits does not exceed 50, and at least one non-zero digit must be present.

Output format:

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

Input sample:

2 2 0 0 0 3 0 0 1 0
Output example:

10015558

The meaning of the question:
Let you enter ten numbers, the 10 numbers indicate the number of times each number appears. Instead of using these ten numbers to form the smallest number.
For example: 2 2 0 0 0 3 0 0 1 0
means that the number "0" appears twice, the number "1" appears twice, the number "5" appears 3 times, and so on.
Instead of using the above numbers to form the smallest number: 1000000223

Idea:
Use arr[10] to store the number of occurrences of each number.
1. Since the array subscript is increasing, we find the first non-zero subscript, which means the first digit. Output 1 directly. Then output arr[0] "0".
2. If the first non-zero number> 1, then output all the smallest non-zeros immediately after zero
3. Output other numbers in the order of the array subscript.

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    
    
	int arr[10]={
    
    0};
	
	//输入部分:输入十个数字 
	for(int i=0;i<10;i++)
	{
    
    
		cin>>arr[i];
	} 
    //逻辑处理部分
	
	//找到最小非零整数 ,输出该数,作为第一位 
	for(int i=1;i<10;i++) 	
	{
    
    
		if(arr[i]>0)
		{
    
    
			cout<<i;
			arr[i]--;  //我们首先要输出第一位非零数字,所以只需count-1,后面还要继续输出该数。 
			break;
		}
	}
	for(int i=0;i<arr[0];i++)
	{
    
    
		cout<<"0";
	}
	
	for(int i=1;i<10;i++)  //控制下标位置 
	{
    
    
		for(int j=0;j<arr[i];j++) //每一下标对应的控制输出个数 
		{
    
    
			if(arr[i]!=0)
			{
    
    
				cout<<i;
			} 
		}
	}
}

result:

Insert picture description here

Guess you like

Origin blog.csdn.net/SKMIT/article/details/113789029