ccf 201503-2 Given n integers, please count the number of occurrences of each integer, and output in descending order of the number of occurrences

ccf 201503-2 Given n integers, count the number of occurrences of each integer, and output in descending order of the number of occurrences

ccf 201503-2

topic

Topic:
Problem description
  Given n integers, please count the number of occurrences of each integer, and output them in order of the number of occurrences.
Input format
  The first line of input contains an integer n, which represents the number of given numbers.
  The second line contains n integers, and adjacent integers are separated by a space to indicate the given integer.
Output format
  outputs multiple lines, each line contains two integers, respectively representing a given integer and the number of times it appears. Output in descending order of occurrence. If two integers appear the same number of times, the smaller value is output first, and then the larger value is output.
Sample input
12
5 2 3 3 1 3 4 2 5 2 3 5
Sample output
3 4
2 3
5 3
1 1
4 1
Evaluation use case scale and convention
  1 ≤ n ≤ 1000, the number given is not more than 1000 Non-negative integer.

Code:

Written according to my own understanding, relatively stupid method, score 80

#include<stdio.h>
int main()
{
	int n;
	scanf("%d",&n);
	int a[n] = {0};
	int b[n];
	for(int i=0;i<n;i++) 
		b[i] = 1;
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	
	for(int l=0;l<n-1;l++) 
		for(int i=0;i<n-1-l;i++)
		{
			if(a[i]>a[i+1])
			{
				int temp;
				temp = a[i+1];
				a[i+1] = a[i];
				a[i] = temp;
			}
		}			
	for(int i=0;i<n-1;i++)
	{
		for(int j=i+1;j<n;j++)
		{
			if(a[i] == a[j])
				b[i] += 1;
			
		}
		
	}
	
	for(int i=0;i<n;i++)
		printf("%d ",a[i]);
	printf("\n");
	for(int i=0;i<n;i++)
		printf("%d ",b[i]);
	printf("\n");
	
	int j = 0;
	int max = 0;
	int bian[n] = {0};	
	for(int p=0;p<n;p++)
	{
		if(bian[p] == 1)
		{
			continue;
		}
		for(int i=0;i<n;i++)
		{
			if(bian[p] == 1)
				continue;
			if(b[i]>max && bian[i] == 0)
			{
				max = b[i];
				j = i;
			}
			if(i == n-1)
			{
				
				printf("%d %d\n",a[j],max);
				max = 0;
				for(int k=0;k<n;k++)
					if(a[k] == a[j])
						bian[k] = 1;
			}
			
		}	
	}
	
	return 0;
}

Continue to improve the code afterwards.

Guess you like

Origin blog.csdn.net/qq_41018465/article/details/109231901