PTA7-9 group minimum number

PTA7-9 minimum number of questions

Question type

Simplicity

Realization idea

  1. First use an array to store the read data, the data is the number of 0-9
  2. Enter the loop, pay attention to start from 1 first, because zero is skipped
  3. Find the first non-zero element with a non-zero number
  4. Enter the loop, output the element

Implementation code

#include <iostream>
using namespace std;

int main()
{
    
    
	int num[10];
	for (int i = 0; i < 10; i++)
	{
    
    
		cin >> num[i];
	}

	for (int i = 0; i < 10; i++)
	{
    
    
		
			if (num[i] != 0 && i != 0)
			{
    
    
				cout << i;
				num[i]--;
				break;
			}
	}
	for (int i = 0; i < 10; i++)
	{
    
    
		for (int k = 0; k < num[i]; k++)
		{
    
    
			cout << i;
		}
	}
	
	return 0;
}

Error-prone

If you don’t read the question carefully, just output the first non-zero element once. This point is the first and last test point of the question.

Guess you like

Origin blog.csdn.net/weixin_45660543/article/details/109319307