PTA 点赞 Java

PTA 点赞 Java

在这里插入图片描述

用数组保存1000个编号,然后记录下每个编号出现是次数
遍历找出出现次数最多且编号最大的那个

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		int N = Reader.nextInt();
		int K;
		int[] id = new int[1001];
		for (int i = 0; i < N; i++) {
			K = Reader.nextInt();
			for (int j = 0; j < K; j++) {
				id[Reader.nextInt()]++;
			}
		}

		int max = 1;
		for (int i = 2; i < id.length; i++) {
			if (id[max] <= id[i]) {
				max = i;
			}
		}
		System.out.println(max +" "+ id[max]);
	}

}

// Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static String nextLine() throws IOException {
		return reader.readLine();
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static char nextChar() throws IOException {
		return next().toCharArray()[0];
	}

	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}
}

发布了27 篇原创文章 · 获赞 2 · 访问量 1475

猜你喜欢

转载自blog.csdn.net/Samil_Hy/article/details/104197593