http://ac.jobdu.com/problem.php?pid=1156
題目1156:誰是你的潛在朋友 方法:利用結構體+Hash函數。1.用結構體來記錄讀者i-1最喜歡的圖書的編號P(其實可以理解爲讀者i)
2.利用Hash函數來構造讀者i-1最喜歡的圖書的編號P一一對應的關係
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#include<iostream> #include<cstdio> #include<cstring> using
namespace
std; struct
H{ int
num; int
P; }; int
main(){ int
n,m,i; H
Hash[210]; while ( scanf ( "%d%d" ,&n,&m)!=EOF){ memset (Hash,0, sizeof (Hash)); int
x; for (i=1;i<=n;i++){ scanf ( "%d" ,&Hash[i].P); Hash[Hash[i].P].num++; } for (i=1;i<=n;i++){ if (Hash[Hash[i].P].num>1){ printf ( "%d\n" ,Hash[Hash[i].P].num-1); } else
printf ( "BeiJu\n" ); } } return
0; } /************************************************************** Problem:
1156 User:
lanjiangzhou Language:
C++ Result:
Accepted Time:10
ms Memory:1520
kb ****************************************************************/ |