L1-034 点赞
微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持。每篇博文都有一些刻画其特性的标签,而你点赞的博文的类型,也间接刻画了你的特性。本题就要求你写个程序,通过统计一个人点赞的纪录,分析这个人的特性。
输入格式:
输入在第一行给出一个正整数N(≤1000),是该用户点赞的博文数量。随后N行,每行给出一篇被其点赞的博文的特性描述,格式为“K F1⋯F**K”,其中 1≤K≤10,F**i(i=1,⋯,K)是特性标签的编号,我们将所有特性标签从 1 到 1000 编号。数字间以空格分隔。
输出格式:
统计所有被点赞的博文中最常出现的那个特性标签,在一行中输出它的编号和出现次数,数字间隔 1 个空格。如果有并列,则输出编号最大的那个。
输入样例:
tex
4
3 889 233 2
5 100 3 233 2 73
4 3 73 889 2
2 233 123
4
3 889 233 2
5 100 3 233 2 73
4 3 73 889 2
2 233 123
输出样例:
tex
233 3
233 3
Solution:
java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
int[] like = new int[1001];
for (int i = 0; i < n; i++) {
String[] input = in.readLine().split(" ");
for (int j = 1; j < input.length; j++) {
like[Integer.parseInt(input[j])]++;
}
}
int max = -1, index = -1;
for (int i = 0; i < like.length; i++) {
if (max <= like[i]) {
max = like[i];
index = i;
}
}
System.out.printf("%d %d", index, max);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
int[] like = new int[1001];
for (int i = 0; i < n; i++) {
String[] input = in.readLine().split(" ");
for (int j = 1; j < input.length; j++) {
like[Integer.parseInt(input[j])]++;
}
}
int max = -1, index = -1;
for (int i = 0; i < like.length; i++) {
if (max <= like[i]) {
max = like[i];
index = i;
}
}
System.out.printf("%d %d", index, max);
}
}