当前位置:网站首页>Pat class B 1023 minimum decimals

Pat class B 1023 minimum decimals

2022-06-23 05:57:00 Octopus bro

1023. Group a minimum number (20)


Given number 0-9 There are several of them . You can arrange these numbers in any order , But all must be used . The goal is to make the final number as small as possible ( Be careful 0 Can't be the first ). for example : Given two 0, Two 1, Three 5, One 8, The smallest number we get is 10015558.

Now give the number , Please write a program to output the smallest number that can be composed .

Input format :

Each input contains 1 Test cases . Each test case is given on one line 10 Nonnegative integers , Order means we have numbers 0、 Numbers 1、…… Numbers 9 The number of . Integers are separated by a space .10 The total number of numbers does not exceed 50, And at least have 1 A non 0 The number of .

Output format :

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

sample input :
2 2 0 0 0 3 0 0 1 0
sample output :
10015558


Ideas : First output a minimum non-zero position , Then judge whether there is 0, If you have any 0 Then output all 0, Then output all non-zero bits in ascending order

One 、 Starting variable

1.count【10】, Statistics 0-9 The number of

2.flag, Used to mark whether there is 0

Two 、 operation

1. Statistics 0-9 Respective quantity

2. from 1 Start to output , First output the first non-zero number , Output one bit

3. Judge flag Is it 1, if 1 It means there is 0, Then output all 0, Then output the remaining non-zero numbers from small to large

3、 ... and 、 Code

#include "stdio.h"

int main()
{
	int count[10] = {0};
	int i = 0 ,j = 0,k = 0;
	for(i = 0; i < 10; i++)
	{
		scanf("%d",&count[i]);
	}
	int flag = 0;// Used to mark whether there is 0 
	if(count[0] != 0)
	{
		flag = 1;// If you have any 0 be flag Position as 1 
	}
	for(i = 1; i < 10; i++)
	{
		if(count[i] != 0)// from 1 To traverse the , When a non-zero bit is encountered, the output starts  
		{
			for(j = 0; j < count[i]; j++)
			{
				printf("%d",i);// Output one bit first , Then judge whether there is 0 
				if(flag == 1)// If there is zero , Then all 0 Output , If there is 2 individual 1,3 individual 0, Then output a 1 Then output all zero outputs and then output the rest 1 
				{
					for(k = 0; k < count[0]; k++)
					{
						printf("0");
					}
					flag = 0;
				}
				// After that, the remaining non-zero bits will be output  
			}
		}
	}
	return 0;
}


原网站

版权声明
本文为[Octopus bro]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230410347568.html