201312-1出现次数最多的数

题目:

代码:

 import java.util.Scanner;

public class LargestNumber {
    
    public static void main(String[] args) {
        int n;
        int[] values;
        int[] count;
        Scanner scan = new Scanner(System.in);
        
        n = scan.nextInt();
        values = new int[n+1];
        count = new int[n+1];
        
        for(int t=1;t<=n;t++)
            count[t] = 0;
        
        for(int k=1;k<=n;k++)
            values[k] = scan.nextInt();
        
        for(int i=1;i<=n-1;i++) {
            if(count[i] == Integer.MIN_VALUE)
                continue;
            for(int j=i+1;j<=n;j++) {
                if(values[i]==values[j]) {
                    count[i]++;
                    count[j] = Integer.MIN_VALUE;
                }
            }
        }
        
        int temp = 1;
        for(int s=1;s<=n;s++) {
            if(count[s]>count[temp]) {
                temp = s;
            }
            if(count[s]==count[temp]) {
                if(values[s]<values[temp])
                    temp = s;
            }
                
        }
        
        System.out.println(values[temp]);
    }

}


主类名称改为Main

1)注意审题,要求两个次数相同,输出数值较小的数
2)学会自己设置输入样例进行测试

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。