Java实现蓝桥杯VIP 算法训练 P0502

Nancy ·
更新时间:2024-09-20
· 845 次阅读

试题 算法训练 P0502

资源限制
时间限制:1.0s 内存限制:256.0MB
  编写一个程序,读入一组整数,这组整数是按照从小到大的顺序排列的,它们的个数N也是由用户输入的,最多不会超过20。然后程序将对这个数组进行统计,把出现次数最多的那个数组元素值打印出来。如果有两个元素值出现的次数相同,即并列第一,那么只打印较小的那个值。例如,假设用户输入的是“100 150 150 200 250”,则输出为150。
输入:
  6
  100 150 150 200 200 250
输出:
  150

package 第十次模拟; import java.util.Scanner; public class P0502 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int [] num = new int [n]; for (int i = 0; i < n; i++) { num[i]=sc.nextInt(); } sc.close(); int count=1; int temp=num[0]; int max =1; for (int i = 1; i max){ temp=num[i-1]; max=count; } count=1; } else{ count++; } } if(count>max){ temp=num[n-1]; max=count; } System.out.println(temp); } }
作者:南 墙



JAVA 训练 算法 502

需要 登录 后方可回复, 如果你还没有账号请 注册新账号