当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友。本题就要求你找出那些帅到没有朋友的人。
输入格式:
输入第一行给出一个正整数N(≤100),是已知朋友圈的个数;随后N行,每行首先给出一个正整数K(≤1000),为朋友圈中的人数,然后列出一个朋友圈内的所有人——为方便起见,每人对应一个ID号,为5位数字(从00000到99999),ID间以空格分隔;之后给出一个正整数M(≤10000),为待查询的人数;随后一行中列出M个待查询的ID,以空格分隔。
注意:没有朋友的人可以是根本没安装“朋友圈”,也可以是只有自己一个人在朋友圈的人。虽然有个别自恋狂会自己把自己反复加进朋友圈,但题目保证所有K超过1的朋友圈里都至少有2个不同的人。
输出格式:
按输入的顺序输出那些帅到没朋友的人。ID间用1个空格分隔,行的首尾不得有多余空格。如果没有人太帅,则输出No one is handsome。
注意:同一个人可以被查询多次,但只输出一次。
输入样例1:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
8
55555 44444 10000 88888 22222 11111 23333 88888
输出样例1:
10000 88888 23333
输入样例2:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
4
55555 44444 22222 11111
输出样例2:
No one is handsome
传送门 https://pintia.cn/problem-sets/994805046380707840/problems/994805117167976448
用一个大小为100000的数组存储每个人是帅还是不帅,默认全部都是帅的人,在输入数据的时候,如果某个朋友圈的人数大于1,则这个朋友圈里的人都不帅,全部标记下。查找的时候直接利用数组的随机存储特性快速判断是否帅即可。
但是由于数据量过大,我使用的是Java,在最后一个测试点超时了。
如果有人有更好的方法,希望能留言告诉我下,感激不尽!
觉得我的思路有用的话,还请点个赞鼓励一下,我明白不是一个人在前进。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
Writer.init(System.out);
slove();
Writer.close();
}
// 解答入口
static void slove() throws IOException {
int n = Reader.nextInt();
int[] map = new int[100000];
while (n > 0) {
int k = Reader.nextInt();
if (k == 1) {
// 没有朋友人的,帅,但是默认就是帅,不需要修改
Reader.nextInt();
} else {
// 有朋友人的,不帅!
while (k > 0) {
int id = Reader.nextInt();
map[id] = 1;
k--;
}
}
n--;
}
int m = Reader.nextInt();
// 判断是否有帅的人
boolean handsome = false;
while (m > 0) {
int query = Reader.nextInt();
if (map[query] == 0) {
// 由于可能有人会被查询多次,则查过以后就不帅了
map[query] = 1;
// 要格式化输出,补全0
if (handsome) {
Writer.print(String.format(" %05d", query));
} else {
Writer.print(String.format("%05d", query));
handsome = true;
}
}
m--;
}
if (!handsome) {
Writer.print("No one is handsome");
}
}
}
// 以下是输入输出模板和思路逻辑无关
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()) {
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 long nextLong() throws IOException {
return Long.parseLong(next());
}
static char nextChar() throws IOException {
return next().toCharArray()[0];
}
static float nextFloat() throws IOException {
return Float.parseFloat(next());
}
static Double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
class Writer {
static BufferedWriter writer;
static void init(OutputStream outputStream) {
writer = new BufferedWriter(new OutputStreamWriter(outputStream));
}
static void print(Object object) throws IOException {
writer.write(object.toString());
}
static void println(Object object) throws IOException {
writer.write(object.toString());
writer.write("\n");
}
static void close() throws IOException {
// TODO Auto-generated method stub
writer.close();
}
}
标签:Java,import,Reader,IOException,020,L1,static,throws,朋友圈
来源: https://blog.csdn.net/Samil_Hy/article/details/104760757