重制代码
// #include <cstdio>
// #include <cstring>
// // 寻找喜欢同一本书的潜在朋友
// int main() {
// int N, M, book[201]; // N个读者,M本书,book表示读者喜欢的书M的编号
// memset(book, -1, sizeof(book));
// while (scanf("%d%d", &N, &M) != EOF) {
// for (int i = 0; i < N; i++) {
// scanf("%d", &book[i]);
// }
// for (int i = 0; book[i] != -1; i++) {
// int temp = book[i];
// int cnt = -1; // 因为会把自己再算一遍,因此从-1开始
// for (int j = 0; book[j] != -1; j++) {
// if (book[j] == temp)
// cnt++;
// }
// if (cnt) {
// printf("%d\n", cnt);
// }
// else {
// printf("BeiJu\n");
// }
// }
// }
// return 0;
// }
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
int main() {
int n, m; // n个读者,m本书
int booknum;
map<int, int> reader; // 键是读者编号i,值是喜欢的书的编号
int ans[201]; // 该读者有几个潜在朋友
while (scanf("%d%d", &n, &m) != EOF) {
for (int i = 1; i <= n; i++) {
scanf("%d", &booknum);
reader[i] = booknum;
}
for (int i = 1; i <= n; i++) {
int fav = reader[i]; // 要查找的i号读者喜欢书的编号
int cnt = 0;
for (auto it = reader.begin(); it != reader.end(); it++) {
if (it->second == fav) {
cnt++;
}
}
ans[i] = cnt - 1; // 自己不算,当没有其他人喜欢同一本书时,存入0
}
for (int i = 1; i <= n; i++) {
if (!ans[i]) {
printf("BeiJu\n");
}
else {
printf("%d\n", ans[i]);
}
}
}
return 0;
}

京公网安备 11010502036488号