Last active
May 18, 2020 17:56
-
-
Save okumurakengo/368bd5dc750a96ecbcff9194c505af57 to your computer and use it in GitHub Desktop.
安定結婚問題練習
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define N 3 /* 各性の人数 */ | |
int | |
/* 添字(女性1..3) => 値(男性1..3) */ | |
boy[4] = {0, 0, 0, 0} | |
/* 各女性の好み */ | |
// rank[女性1..3][男性1..3] = ランク | |
// 女性3 -> 男性1:1位 男性2:3位 男性3:2位 | |
, rank[4][4] = {{}, {4, 1, 2, 3}, {4, 3, 2, 1}, {4, 1, 3, 2}} | |
/* 各男性の好み */ | |
// girl[男性1..3][ランク] = 女性1..3 | |
// 男性1 -> 女性1:2位 女性2:3位 女性3:1位 | |
, girl[4][4] = {{}, {4, 3, 1, 2}, {4, 2, 3, 1}, {4, 3, 2, 1}} | |
, position[4] = {0, 0, 0, 0}; | |
int main(void) | |
{ | |
int b, g, r, s, t; | |
for (b = 1; b <= N; b++) { | |
// b,sは男1..3の値 | |
s = b; | |
while (s != 0) { | |
// 男性の好みの女性をランクが上の 女性1..3 を取得 | |
// 男性単位で2週目以降は次のランクの 女性1..3 | |
g = girl[s][++position[s]]; | |
// "女性に対しての男性のランク" | |
// と | |
// "女性に対しての男性の暫定ランク(暫定ランクがない場合は初期値4になる)" | |
// を比較する | |
// ----- | |
// 男性の暫定ランクではない場合は既出の 女性1..3 => 男性1..3 のペア つまり好みが被っている | |
// 暫定ランクよりも今回のランクの方が上位の場合は上書き処理に入る | |
if (rank[g][s] < rank[g][boy[g]]) { | |
// 暫定ペアを一時保存(暫定ペアがない場合は初期値4になる) | |
t = boy[g]; | |
// 暫定で 女性1..3 => 男性1..3 を保存 | |
boy[g] = s; | |
// 次に処理する男性1..3 | |
// 暫定ランクよりも上位だった場合はもう一度その男性でペアを保存しに行く | |
s = t; | |
} | |
} | |
} | |
for (g = 1; g <= N; g++) { | |
printf("女 %d - 男 %d\n", g, boy[g]); | |
} | |
return 0; | |
} |
Author
okumurakengo
commented
May 18, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment