CCF CSP Brush Question Record 1-the most frequent number of occurrences in 201312

Question number: 201312-1
Question name: Most frequent occurrences
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:

Problem Description

  Given n positive integers, find the number that appears most frequently among them. If there are multiple such numbers, please output the smallest one.

Input format

  The first line of input has only one positive integer n (1 ≤ n ≤ 1000), which represents the number of digits.
  The second line of input has n integers s1, s2, …, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n). Adjacent numbers are separated by spaces.

Output format

  Output the number with the most occurrences among these n times. If there are multiple such numbers, output the smallest one.

Sample input

6
10 1 10 20 30 20

Sample output

10

 

import java.util.Scanner;

public class 出现次数最多的数201312_1 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int len=sc.nextInt();
		int[] a=new int[10001];
		for(int i=1;i<=len;i++){
			int num=sc.nextInt();
			a[num]+=1;
		}
//		for(int i=1;i<=10000;i++){
//			System.out.print(a[i]);
//		}
		int max=a[1];
		int res=1;
		int c;
		for(c=1;c<=10000;c++){
			if(a[c]>max){
				max=a[c];//这个一定要加上的原因是  [6:7  7:7  8:7]应该返回6 如果只输出序号 则返回7了
				res=c;
			}
		}
		System.out.println(res);

	}

}

 

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108244945