-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.c
79 lines (68 loc) · 1.97 KB
/
1.c
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
#include <stdio.h>
#include <stdbool.h>
#define S 0
int main() {
int n;
printf("Enter the number of men/women:");
scanf("%d", &n);
int mlist[n + 1][n + 1];
int wlist[n + 1][n + 1];
int mancurrentmatch[n + 1];
int womencurrentmatch[n + 1];
int mannextproposal[n + 1];
printf("Enter the women's preference list:\n");
for (int i = 1; i <= n; i++) {
womencurrentmatch[i] = S;
printf("For women %d:", i);
for (int j = 1; j <= n; j++) {
scanf("%d", &wlist[i][j]);
}
}
printf("Enter the men preference list:\n");
for (int i = 1; i <= n; i++) {
mancurrentmatch[i] = S;
mannextproposal[i] = 1;
printf("For men %d:", i);
for (int j = 1; j <= n; j++) {
scanf("%d", &mlist[i][j]);
}
}
bool freemanavail = true;
int m = 1;
while (freemanavail) {
freemanavail = false;
int w = mlist[m][mannextproposal[m]++];
if (womencurrentmatch[w] == S) {
womencurrentmatch[w] = m;
mancurrentmatch[m] = w;
} else {
bool itisbetterproposal = false;
for (int y = 1; y <= n; y++) {
if (wlist[w][y] == womencurrentmatch[w]) {
break;
}
if (wlist[w][y] == m) {
itisbetterproposal = true;
break;
}
}
if (itisbetterproposal) {
mancurrentmatch[womencurrentmatch[w]] = S;
womencurrentmatch[w] = m;
mancurrentmatch[m] = w;
}
}
for (int x = 1; x <= n; x++) {
if (mancurrentmatch[x] == S) {
m = x;
freemanavail = true;
break;
}
}
}
printf("Stable matching:\n");
for (int i = 1; i <= n; i++) {
printf("Couple: man %d and woman %d\n", i, mancurrentmatch[i]);
}
return 0;
}