L1-020 帥到沒朋友 (20分) Java

當芸芸衆生忙着在朋友圈中發照片的時候,總有一些人因爲太帥而沒有朋友。本題就要求你找出那些帥到沒有朋友的人。

輸入格式:

輸入第一行給出一個正整數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();
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章