1075: 众数问题

来源:互联网 发布:windows 10不能打字 编辑:程序博客网 时间:2024/06/18 04:28

题目

Description

问题描述:
给定含有n个元素的多重集合S,每个元素在S中出现的次数称为该元素的重数。多重集S中重数最大的元素称为众数。
例如,S={1,2,2,2,3,5}。多重集S的众数是2,其重数为3。
编程任务:
对于给定的由n 个自然数组成的多重集S,编程计算S 的众数及其重数。

Input

第1行多重集S中元素个数n(n<=50000);接下来的n 行中,每行有一个自然数。

Output

输出文件有2 行,第1 行给出众数,第2 行是重数。(如果有多个众数,只输出最小的)

Sample Input

6
1
2
2
2
3
5
Sample Output

2
3


代码块

import java.util.Arrays;import java.util.Scanner;public class Main{    public static void main(String[] args) {        Scanner cn = new Scanner(System.in);        int n = cn.nextInt();        int[] a = new int[n];        int[] b = new int[n];        Arrays.fill(b, 0);        for (int i = 0; i < n; i++) {            a[i] = cn.nextInt();        }        for(int i = 0; i < n; i++){            for(int j = 0; j < n; j++){                if(a[i]==a[j]) b[i]++;            }        }        int max = 0;        int s = 0;        for(int i = 0; i < n; i++){            if(b[i]>max){                max = b[i];                s = a[i];            }        }        System.out.println(s);        System.out.println(max);    }}
0 0
原创粉丝点击