java首先输入大于1小于100的整数,作为一组无序整数的个数,接着输入这组整数,编程输出其中出现次数最多的整数及其出现次数,如果有多个整数,都要输出,并且以升序输出
时间: 2024-05-27 10:10:42 浏览: 77
丢失的最小正整数leetcode-LeetCode:力码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
Arrays.sort(nums);
int maxCount = 1;
int curCount = 1;
int maxNum = nums[0];
for (int i = 1; i < n; i++) {
if (nums[i] == nums[i - 1]) {
curCount++;
} else {
if (curCount > maxCount) {
maxCount = curCount;
maxNum = nums[i - 1];
}
curCount = 1;
}
}
if (curCount > maxCount) {
maxCount = curCount;
maxNum = nums[n - 1];
}
curCount = 1;
for (int i = 1; i < n; i++) {
if (nums[i] == nums[i - 1]) {
curCount++;
} else {
if (curCount == maxCount) {
System.out.println(nums[i - 1] + " " + maxCount);
}
curCount = 1;
}
}
if (curCount == maxCount) {
System.out.println(nums[n - 1] + " " + maxCount);
}
}
}
阅读全文