自用PTA题目记录0011
以下题目序号并无实际意义
7-11 JAVA-求整数序列中出现次数最多的数
题目作者: 老象 单位: 贵州师范学院 代码长度限制: 16 KB 时间限制: 2400 ms 内存限制: 64 MB
要求统计一个整型序列中出现次数最多的整数及其出现次数。
输入格式: 在一行中给出序列中整数个数N(0<N≤1000),依次给出N个整数,每个整数占一行。
输出格式: 在一行中输出出现次数最多的整数及其出现次数,数字间以空格分隔。题目保证这样的数字是唯一的。
输入样例1:
10
3
2
-1
5
3
4
3
0
3
2
输出样例1:
3 4
代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
ArrayList<Integer> nList = new ArrayList<>();
int maxNum = 0;
int maxfrq = 1;
for (int i = 1; i <= N; i++) {
nList.add(sc.nextInt());
}
maxNum = nList.get(0);
while (!nList.isEmpty()) {
int tempNum = nList.get(0);
int tempfrq = Collections.frequency(nList, nList.get(0));
if (tempfrq > maxfrq) {
maxNum = tempNum;
maxfrq = tempfrq;
}
for (int i = 0; i < nList.size(); i++) {
if (nList.get(i).equals(tempNum)) {
nList.remove(i);
}
}
}
System.out.println(maxNum + " " + maxfrq);
sc.close();
}
}
总结
java做这题效率并不高,只是正好在用java(懒
PTA提交通过截图