题目解析
判断给定的m个数中 是否为没有朋友的人 要是没有朋友的人 就输出这个人
解题思路
可以按照题意 利用 set 去存储 每个人朋友数大于1的这个人的所有朋友
然后再去这个set中看是否出现过
输出的时候也需要用set判重
但是使用set这样子的话,不够快
可以用数组模拟set,即可通过本题
代码
set
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n = sc.nextInt();
TreeSet<Integer> shu = new TreeSet<Integer>(); // 存储所有朋友的编号
while (n-- > 0)
{
int t = sc.nextInt();
for (int i = 1; i <= t; i++)
{
int x = sc.nextInt();
if (t != 1) // 代表有朋友的
shu.add(x);
}
}
int m = sc.nextInt();
ArrayList<Integer> res = new ArrayList<Integer>(); // 存储答案
TreeSet<Integer> ress = new TreeSet<Integer>(); // 这个朋友是否添加过了
while (m-- > 0)
{
int x = sc.nextInt();
// 朋友圈没有这个朋友的 并且 没有添加过的
if (!shu.contains(x) && ress.add(x))
res.add(x);
}
if (res.size() == 0)
out.println("No one is handsome");
int pos = 0;
for (int i : res)
{
if (pos++ != 0)
out.print(" ");
out.printf("%05d", i);
}
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}
数组模拟set
import java.io.*;
import java.util.*;
public class Main
{
static int N = (int) 1e5 + 10;
static boolean a[] = new boolean[N];
static boolean b[] = new boolean[N];
static String format(int x)
{
StringBuilder s = new StringBuilder(String.valueOf(x));
int n = s.length();
for (int i = 0; i < 5 - n; i++) s.insert(0, "0");
return s.toString();
}
public static void main(String[] args) throws IOException
{
int n = ini();
while (n-- > 0)
{
int t = ini();
for (int i = 1; i <= t; i++)
{
int x = ini();
if (t != 1) // 代表有朋友的
a[x] = true;
}
}
int m = ini();
StringBuilder sb = new StringBuilder();
int pos = 0;
while (m-- > 0)
{
int x = ini();
// 朋友圈没有这个朋友的 并且 没有添加过的
if (!a[x] && !b[x])
{
b[x] = true;
if (pos++ != 0) sb.append(" ");
sb.append(format(x));
}
}
if (pos == 0) out.println("No one is handsome");
else out.println(sb);
out.flush();
out.close();
}
static StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
static int ini() throws IOException
{
sc.nextToken();
return (int) sc.nval;
}
}