当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友。本题就要求你找出那些帅到没有朋友的人。
输入格式:
输入第一行给出一个正整数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
C++代码,未超时,原理看java代码,原理一样:
#include
#include
#include
using namespace std;
int main()
{
int N;
scanf("%d", &N);
int friends[100000];
memset(friends,0,sizeof(friends));
while (N--) {
int k;
scanf("%d", &k);
if (k >= 2) {
while (k--) {
int per;
scanf("%d", &per);
friends[per] = 1;
}
} else {
int temp;
scanf("%d", &temp);
}
}
int M;
scanf("%d", &M);
bool have_one = false;
while (M-- > 0) {
int query;
scanf("%d", &query);
if (friends[query] == 0) {
if (have_one) {
printf(" ");
}
have_one = true;
printf("%05d", query);
friends[query] = -1;
}
}
if (!have_one) {
printf("No one is handsome");
}
return 0;
}
Java 代码,Scanner超时,BufferReader也超时:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int[] friends = new int[100000];
while (N-- > 0) {
int k = scanner.nextInt();
// 只存储有2个人及以上的朋友圈
if (k >= 2) {
while (k-- > 0) {
int fiend = scanner.nextInt();
// 标记存储起来
friends[fiend] = 1;
}
} else {
// 否则是只有自己一个人在朋友圈,不保存
scanner.nextInt();
}
}
int M = scanner.nextInt();
boolean have_one = false; // 是否存在单身的
while (M-- > 0) {
int query = scanner.nextInt();
// 不在所有朋友圈中的人
if (friends[query] == 0) {
// 如果之前已经有单身,则先打印空格
// 第一个是单身的则不用打印
if (have_one) {
System.out.print(" ");
}
have_one = true;
System.out.printf("%05d", query);
// 每个单身的只输出一次,避免重复输出
friends[query] = -1;
}
}
// 如果待查询的当中没有单身
if (!have_one) {
System.out.print("No one is handsome");
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bReader = new BufferedReader(
new InputStreamReader(System.in));
int N = Integer.parseInt(bReader.readLine());
int[] friends = new int[100000];
while (N-- > 0) {
String[] data = bReader.readLine().split(" ");
int k = Integer.parseInt(data[0]);
// 只存储有2个人及以上的朋友圈
if (k >= 2) {
for (int i = 1; i <= k; i++) {
int fiend = Integer.parseInt(data[i]);
// 标记存储起来
friends[fiend] = 1;
}
}
// 否则是只有自己一个人在朋友圈,不标记
}
int M = Integer.parseInt(bReader.readLine());
boolean have_one = false; // 是否存在单身的
// 读取待查询的数据,用空格分割
String[] querieds = bReader.readLine().split(" ");
for (int i = 0; i < M; i++) {
int queried = Integer.parseInt(querieds[i]);
// 不在所有朋友圈中的人
if (friends[queried] == 0) {
// 如果之前已经有单身,则先打印空格
// 第一个是单身的则不用打印
if (have_one) {
System.out.print(" ");
}
have_one = true;
System.out.printf("%05d", queried);
// 每个单身的只输出一次,避免重复输出
friends[queried] = -1;
}
}
// 如果待查询的当中没有单身
if (!have_one) {
System.out.print("No one is handsome");
}
bReader.close();
}
}
python代码:
N = int(input())
friends = []
while N > 0:
one_friends = input().split()
del one_friends[0]
friends += one_friends
N -= 1
M = int(input())
queries = input().split()
no_one = True
for query in set(queries):
if query not in set(friends):
print(query, end=" ")
no_one = False
if no_one:
print('No one is handsome')