#include
using
namespace
std;
bool finish_or_not(int, int *);
bool current_male_is_better(int num, int *male_rank_in_female, int current, int chasing);
int main()
for (int i = 0; i < num; i++)
}for (int i = 0; i < num; i++)
}*/int female_rank_in_male[5][5] = , , , , };
int male_rank_in_female[5][5] = , , , , };
//男人和女人正在約會的物件
int *date_of_male = new
int[num];
int *date_of_female = new
int[num];
for (int i = 0; i < num; i++)
//男人追求過的女人的數量
int *num_of_chased_female = new
int[num];
for (int i = 0; i < num; i++)
while (finish_or_not(num, date_of_male) == false)
else
if (current_male_is_better(num, male_rank_in_female[female_to_chase - 1], date_of_female_to_chase, i + 1))
else
num_of_chased_female[i]++;//該男人追過的女人數量加1
cout
<< "男人 "
<< i + 1
<< " 追過的女人數目是 "
<< num_of_chased_female[i] << endl;
}else}}
//最後的輸出
cout
<< endl <<"男人:";
for (int i = 0; i < num; i++)
cout
<< endl << "女人:";
for (int i = 0; i < num; i++)
cout
<< endl << endl;
/*//刪除陣列
for (int i = 0; i < num; i++)
delete female_rank_in_male;
delete male_rank_in_female;*/
delete date_of_male;
delete date_of_female;
delete num_of_chased_female;
return0;}
bool finish_or_not(int num, int *date_of_male)
cout
<< endl << endl;
for (int i = 0; i < num; i++)
}cout
<< "已完全匹配!!!"
<< endl;
return
true;
}//比較某女人現在的約會物件和追求者哪個在她心目中排行更高
//注意陣列引數是一維(一位女人的優先表而不是所有女人的優先表)
bool current_male_is_better(int num, int *male_rank_in_female, int current, int chasing)
if (male_rank_in_female[i] == chasing)
}cout
<< "現任排名是 "
<< rank_of_current << " 追求者排名是 "
<< rank_of_chasing << endl;
if (rank_of_current < rank_of_chasing)
return
true;
else
return
false;
}
#include
using
namespace
std;
bool finish_or_not(int, int *);
bool current_male_is_better(int num, int *male_rank_in_female, int current, int chasing);
int main(), , , , };
int male_rank_in_female[5][5] = , , , , };
for (int i = 0; i < num; i++)
cout
<< endl;
}cout
<< endl;
for (int i = 0; i < num; i++)
cout
<< endl;
}//男生和妹子正在約會的物件
int *date_of_male = new
int[num];
int *date_of_female = new
int[num];
for (int i = 0; i < num; i++)
//男生追求過的妹子的數量
int *num_of_chased_female = new
int[num];
for (int i = 0; i < num; i++)
doelse
if (date_of_female_to_chase == 0)
else
if (current_male_is_better(num, male_rank_in_female[female_to_chase - 1], date_of_female_to_chase, i + 1))
else
num_of_chased_female[i]++;//該男生追過的妹子數量加1
}else
}} while (finish_or_not(num, date_of_male) == false);
//最後的輸出
cout
<< endl << "┌───故事的結局───┐"
<< "│ │"
<< endl;
for (int i = 0; i < num; i++)
cout
<< "│ │"
<< endl;
cout
<< "└───────────┘"
<< endl << endl;
delete date_of_male;
delete date_of_female;
delete num_of_chased_female;
system("pause");
return0;}
bool finish_or_not(int num, int *date_of_male)
}cout
<< endl << "已全部脫離單身狗行列!!!"
<< endl;
return
true;
}//比較某妹子現在的物件和追求者哪個在她心目中排行更高
//注意陣列引數是一維(一位妹子的優先表而不是所有妹子的優先表)
bool current_male_is_better(int num, int *male_rank_in_female, int current, int chasing)
if (male_rank_in_female[i] == chasing)
}cout
<< "在妹子心目中現任排名是 "
<< rank_of_current << " ,而追求者排名則是 "
<< rank_of_chasing << endl;
if (rank_of_current < rank_of_chasing)
return
true;
else
return
false;
}
穩定婚姻匹配演算法
問題概述 n名男青年和n名女青年參加乙個相親會。經過了解,每名男青年在心目中都對所有女青年作了乙個排序,而每名女青年同樣也對男青年作了排序。問這些男女青年之間怎樣才能進行理想的配對。給出男青年和女青年的偏好矩陣 演算法求解過程 演算法設計 1 根據男性的偏好矩陣來進行遍歷 2 在男性的偏好矩陣中依次...
穩定婚姻匹配
要求按照書中 page 5 的 g s 演算法寫乙個匹配程式 由男方發起邀請 第一行資料表示 man woman 的數目 n 接下來的資料中,第乙個 n n 的資料塊表示 man 的優先列表 另乙個 n n 的資料塊表示 woman 的優先列表 5 man woman 的數目 n 2 1 4 5 3...
穩定婚姻問題
穩定婚姻是組合數學裡面的乙個問題。問題大概是這樣 有乙個社團裡有n個女生和n個男生,每位女生按照她的偏愛程度將男生排序,同時每位男生也按照自己的偏愛程度將女生排序。然後將這n個女生和n個男生配成完備婚姻。如果存在兩位女生a和b,兩位男生a和b,使得a和a結婚,b和b結婚,但是a更偏愛b而不是a,b更...