-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.1(v1).cpp
120 lines (110 loc) · 2.41 KB
/
11.1(v1).cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
using namespace std;
#define maxsize 32767
typedef struct phone *Phone;
typedef struct phone{
long int mobile;
int callnum=0;
Phone next;
}phone;
typedef struct book{
int phonenumber;
Phone head;
}book,*Book;
int nextprime(int n);
Book createTable(int n);
int Hash(long int key,int p);
Phone Find(Book B,long int key);
bool Insert(Book B,long int key);
void scanandoutput(Book B);
int main(){
Book H;
int n;
cin>>n;
long int s;
H=createTable(n*2);
for(int i=0;i<n;i++){
cin>>s;
Insert(H,s);
cin>>s;
Insert(H,s);
}
scanandoutput(H);
return 0;
}
Book createTable(int n){
Book B;
B=(Book)malloc(sizeof(book));
B->phonenumber=nextprime(n);
B->head=(Phone)malloc(B->phonenumber*sizeof(phone));
for(int i=0;i<B->phonenumber;i++){
B->head[i].mobile=0;
B->head[i].next=NULL;
B->head[i].callnum=0;
}
return B;
}
int nextprime(int n){
n=(n%2==0)?n+1:n;
for(;n<maxsize;n+=2){
int flag=1;
for(int i=3;i*i<=n;i+=2){
if(n%i==0) {flag=0;break;}
}
if(flag==1) break;
}
return n;
}
int Hash(long int key,int p){
int s=key%10000;
return s%p;
}
Phone Find(Book B,long int key){
int pos=Hash(key,B->phonenumber);
Phone p=B->head[pos].next;
while(p&&p->mobile!=key) p=p->next;
return p;
}
bool Insert(Book B,long int key){
Phone p,newcell;
int pos;
p=Find(B,key);
if(!p){
newcell=(Phone)malloc(sizeof(phone));
newcell->mobile=key;
newcell->callnum=1;
pos=Hash(key,B->phonenumber);
newcell->next=B->head[pos].next;
B->head[pos].next=newcell;
return true;
}
else{
p->callnum++;
return false;
}
}
void scanandoutput(Book B){
int i,maxcnt=0,pcnt=0;
long int minphone;
Phone ptr;
minphone=0;
for(i=0;i<B->phonenumber;i++){
ptr=B->head[i].next;
while(ptr){
if(ptr->callnum>maxcnt){
maxcnt=ptr->callnum;
minphone=ptr->mobile;
pcnt=1;
}
else if(ptr->callnum==maxcnt){
pcnt++;
if(minphone>ptr->mobile){
minphone=ptr->mobile;
}
}
ptr=ptr->next;
}
}
cout<<minphone<<" "<<maxcnt;
if(pcnt>1) cout<<" "<<pcnt;
}