Codeup exercises who are your potential friends

 

Title description

 

    "Smelly similar"-this is the word we like to use when describing friends. Two people being friends usually means that they have many common interests. However, as an otaku, you find that there are not many opportunities for you to get to know each other. Fortunately, you accidentally got a copy of the book borrowing record from the Peking University Library, so you choose to stay up late to program, hoping to discover potential friends.
    First, you sorted out the borrowing records, numbering N readers as 1, 2,..., N, and numbering M books as 1, 2,..., M. At the same time, in accordance with the principle of "smelly similar", people who like to read the same book as you are your potential friends. Your task now is to calculate how many potential friends each person has from this loan record.

enter

 

    Two integers N, M, 2 <= N, M <= 200 in the first line of each case. Next, there are N rows, each of the i-th row (i = 1,2,...,N) has a number, which represents the number P (1<=P<=M) of reader i-1's favorite book

Output

 

    Each case includes N rows, each with a number, and the number in the i-th row indicates how many potential friends reader i has. If i and no one have a favorite book in common, output "BeiJu" (ie tragedy, ^ ^)

Sample input Copy

4 5
2
3
2
1

Sample output Copy

1 
BeiJu 
1 
BeiJu

 

AC code

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 210;
int hashTable[N];
int book_id[N];

int main()
{
    int n,m;
    while (scanf("%d%d",&n,&m) != EOF)
    {
        for (int i = 0;i < n;i++)
        {
            scanf("%d",&book_id[i]);
            hashTable[book_id[i]]++;
        }
        for (int i = 0;i < n;i++)
        {
            if (hashTable[book_id[i]] == 1) printf("BeiJu\n");
            else printf("%d\n",hashTable[book_id[i]] - 1);
        }
        //多组数据输入,需要重新初始化数组,不然容易残留上一次输入的数据
        memset(hashTable,0,sizeof hashTable);
        memset(book_id,0,sizeof book_id);
    }
    return 0;
}

In the following

4 5

2

3

2

1

For example:

Array index: 0 1 2 3 4

book_id:     2    3    2    1

hashTable : 0    1   2    1

The value of the hashTable array represents the number of times the book appears. If the value in the hashTable array is 1, it means that only one person likes the book

if (hashTable[book_id[i]] == 1) printf("BeiJu\n");

Otherwise, output the number of people who like the book together, whose value is equal to hashTable[book_id[i]]-1

else printf("%d\n",hashTable[book_id[i]] - 1);

 

Guess you like

Origin blog.csdn.net/smallrain6/article/details/107337698