7-8 人工智能打招呼
号称具有人工智能的机器人,至少应该能分辨出新人和老朋友,所以打招呼的时候应该能有所区别。本题就请你为这个人工智能机器人实现这个功能:当它遇到陌生人的时候,会说:“Hello X, how are you?”其中 X 是这个人的称呼;而当它再次遇到这个人的时候,会说:“Hi X! Glad to see you again!”
输入格式:
输入首先在第一行中给出一个正整数 N(≤105),随后一行给出 N 个人的编号。即简单起见,我们把每个人的称呼 X 用一个 5 位整数来替代。
输出格式:
对于每个人的编号,按照题面要求在一行中输出人工智能机器人打招呼的内容。
输入样例:
tex
7
00000 99999 00000 12345 00000 12345 00000
7
00000 99999 00000 12345 00000 12345 00000
输出样例:
tex
Hello 00000, how are you?
Hello 99999, how are you?
Hi 00000! Glad to see you again!
Hello 12345, how are you?
Hi 00000! Glad to see you again!
Hi 12345! Glad to see you again!
Hi 00000! Glad to see you again!
Hello 00000, how are you?
Hello 99999, how are you?
Hi 00000! Glad to see you again!
Hello 12345, how are you?
Hi 00000! Glad to see you again!
Hi 12345! Glad to see you again!
Hi 00000! Glad to see you again!
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());
String[] input = in.readLine().split(" ");
int[] book = new int[1000000];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
int x = Integer.parseInt(input[i]);
if (book[x] != 1) {
sb.append("Hello ").append(input[i]).append(", how are you?");
book[x] = 1;
} else {
sb.append("Hi ").append(input[i]).append("! Glad to see you again!");
}
if (i < n - 1) sb.append("\n");
}
System.out.println(sb);
}
}
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());
String[] input = in.readLine().split(" ");
int[] book = new int[1000000];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
int x = Integer.parseInt(input[i]);
if (book[x] != 1) {
sb.append("Hello ").append(input[i]).append(", how are you?");
book[x] = 1;
} else {
sb.append("Hi ").append(input[i]).append("! Glad to see you again!");
}
if (i < n - 1) sb.append("\n");
}
System.out.println(sb);
}
}