import java.util.HashMap; import java.util.Scanner; import java.util.TreeSet; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); String[] name = {"Captain", "Priest", "Cook", "Doctor", "Engineer", "Hunter", "Gunner", "Navigator"}; HashMap<String, Integer> map = new HashMap<>(); map.put(sc.next(), 1); int n = sc.nextInt(); for (int i = 0; i < n; i++) { String s = sc.nextLine(); String ss = s.split(":")[0]; map.put(ss, 1); } TreeSet<String> t = new TreeSet<>(); for (int i = 0; i < 8; i++) { if (!map.containsKey(name[i])) t.add(name[i]); } if (t.size() == 0) System.out.println("Ready"); else { for (Object o : t) System.out.println(o); } } }
时间: 2024-01-26 09:02:08 浏览: 131
Java实验5 集合类.docx
以下是将Java代码转换为C语言代码的示例,为了简化代码,该示例省略了输入和输出部分的具体实现,需要您根据实际情况进行适当修改:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_SIZE 8
int main() {
char name[NAME_SIZE][10] = {"Captain", "Priest", "Cook", "Doctor", "Engineer", "Hunter", "Gunner", "Navigator"};
char input[100];
int n = 0;
scanf("%d", &n);
getchar(); // 处理输入缓冲区残留的换行符
int count = 0;
int map[100] = {0};
for (int i = 0; i < n; i++) {
fgets(input, 100, stdin);
char* p = strtok(input, ":");
int index = -1;
for (int j = 0; j < NAME_SIZE; j++) {
if (strcmp(name[j], p) == 0) {
index = j;
break;
}
}
if (index >= 0) {
map[index] = 1;
count++;
}
}
for (int i = 0; i < NAME_SIZE; i++) {
if (map[i] == 0) {
printf("%s\n", name[i]);
}
}
if (count == NAME_SIZE) {
printf("Ready\n");
}
return 0;
}
```
需要注意的是,Java代码和C语言代码的语法和结构有很大的不同,因此在进行转换时需要仔细思考和分析,以确保转换后的代码逻辑正确、运行稳定。
阅读全文