日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

稳定匹配问题——稳定婚姻算法设计

發布時間:2023/12/4 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 稳定匹配问题——稳定婚姻算法设计 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


圖片源自:美劇《How I met your mother》

****

本代碼帶有詳細的注釋,并在控制臺輸出時詳細地說明了算法的過程,非常有助于新手理解穩定匹配問題和穩定婚姻算法的設計思路。

****

#include?<iostream>
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?num =?5;
? ?/*cout << "輸入數目n:";
? ?cin >> num;
? ?cout << "數目:" << num << endl;

? ?//男人和女人心目中異性的排行
? ?int **female_rank_in_male, **male_rank_in_female;
? ?female_rank_in_male = new int*[num];
? ?male_rank_in_female = new int*[num];
? ?for (int i = 0; i < num; i++){
? ?female_rank_in_male[i] = new int[num];
? ?male_rank_in_female[i] = new int[num];
? ?}

? ?for (int i = 0; i < num; i++){
? ?for (int j = 0; j < num; j++){
? ?cout << endl << "請輸入男人" << i + 1 << "心目中的第" << j + 1 << "名:";
? ?cin >> female_rank_in_male[i][j];
? ?}
? ?}
? ?for (int i = 0; i < num; i++){
? ?for (int j = 0; j < num; j++){
? ?cout << endl << "請輸入女人" << i + 1 << "心目中的第" << j + 1 << "名:";
? ?cin >> male_rank_in_female[i][j];
? ?}
? ?}*/


? ?int?female_rank_in_male[5][5] = { {?2,?1,?4,?5,?3?}, {?4,?2,?1,?3,?5?}, {?2,?5,?3,?4,?1?}, {?1,?4,?3,?2,?5?}, {?2,?4,?1,?5,?3?} };
? ?int?male_rank_in_female[5][5] = { {?5,?1,?2,?4,?3?}, {?3,?2,?4,?1,?5?}, {?2,?3,?4,?5,?1?}, {?1,?5,?4,?3,?2?}, {?4,?2,?5,?3,?1?} };

? ?//男人和女人正在約會的對象
? ?int?*date_of_male =?new?int[num];
? ?int?*date_of_female =?new?int[num];
? ?for?(int?i =?0; i < num; i++){
? ? ? ?date_of_male[i] =?0;
? ? ? ?date_of_female[i] =?0;
? ?}

? ?//男人追求過的女人的數量
? ?int?*num_of_chased_female =?new?int[num];
? ?for?(int?i =?0; i < num; i++){
? ? ? ?num_of_chased_female[i] =?0;
? ?}

? ?while?(finish_or_not(num, date_of_male) ==?false){//如果有男人沒有約會對象
? ? ? ?for?(int?i =?0; i < num; i++){//按序號遍歷所有男人
? ? ? ? ? ?cout?<<?"正在查看男人 "?<< i +?1?<<?" 有沒有約會對象..."?;
? ? ? ? ? ?if?(date_of_male[i] ==?0){//如果某男人沒有約會對象
? ? ? ? ? ? ? ?cout?<<?endl;
? ? ? ? ? ? ? ?cout?<<?"男人 "?<< i +?1?<<?" 沒有約會對象!"?<<?endl;
? ? ? ? ? ? ? ?//該男人準備追的女人(該男人優先表中還沒追求過的排名最高的女人)
? ? ? ? ? ? ? ?int?female_to_chase = female_rank_in_male[i][num_of_chased_female[i]];
? ? ? ? ? ? ? ?//該男人準備追的女人的現任
? ? ? ? ? ? ? ?int?date_of_female_to_chase = date_of_female[female_to_chase -?1];
? ? ? ? ? ? ? ?cout?<<?"男人 "?<< i +?1?<<?" 準備追女人 "?<< female_to_chase <<?",女人現任是 "?<< date_of_female_to_chase <<?endl;

? ? ? ? ? ? ? ?if?(date_of_female_to_chase ==?0){//如果該男人準備追的女人沒有現任
? ? ? ? ? ? ? ? ? ?date_of_male[i] = female_to_chase;//該男人的約會對象變成準備追的女人
? ? ? ? ? ? ? ? ? ?date_of_female[female_to_chase -?1] = i +?1;//男人準備追的女人的約會對象變成該男人
? ? ? ? ? ? ? ? ? ?cout?<<?"男人 "?<< i +?1?<<?" 和女人 "?<< female_to_chase <<?" 在一起了!"?<<?endl;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?else?if?(current_male_is_better(num, male_rank_in_female[female_to_chase -?1], date_of_female_to_chase, i +?1)){
? ? ? ? ? ? ? ? ? ?//如果該男人準備追的女人的現任在女人心目中比該男人更好,則什么也不做
? ? ? ? ? ? ? ? ? ?cout?<<?"男人 "?<< i +?1?<<?" 被女人 "?<< female_to_chase <<?" 拒絕了!"?<<?endl;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?else{//如果該男人比該男人準備追的女人的現任在女人心目中更好
? ? ? ? ? ? ? ? ? ?date_of_male[date_of_female_to_chase -?1] =?0;//該男人準備追的女人的現任回到單身狀態
? ? ? ? ? ? ? ? ? ?date_of_male[i] = female_to_chase;//該男人的約會對象變成準備追的女人
? ? ? ? ? ? ? ? ? ?date_of_female[female_to_chase -?1] = i +?1;//該男人準備追的女人的約會對象變成該男人
? ? ? ? ? ? ? ? ? ?cout?<<?"男人 "?<< i +?1?<<?" 和女人 "?<< female_to_chase <<?" 在一起了!男人 "?<< date_of_female_to_chase <<?" 變成單身!"?<<?endl;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?num_of_chased_female[i]++;//該男人追過的女人數量加1
? ? ? ? ? ? ? ?cout?<<?"男人 "?<< i +?1?<<?" 追過的女人數目是 "?<< num_of_chased_female[i] <<?endl;
? ? ? ? ? ?}
? ? ? ? ? ?else{
? ? ? ? ? ? ? ?cout?<<?"查得已有對象:女人 "?<< date_of_male[i] ?<<?endl;
? ? ? ? ? ?}
? ? ? ?}
? ?}

? ?//最后的輸出
? ?cout?<<?endl?<<"男人:";
? ?for?(int?i =?0; i < num; i++){
? ? ? ?cout?<< i +?1?<<?" ";
? ?}
? ?cout?<<?endl?<<?"女人:";
? ?for?(int?i =?0; i < num; i++){
? ? ? ?cout?<< date_of_male[i] <<?" ";
? ?}
? ?cout?<<?endl?<<?endl;

? ?/*
? ?//刪除數組
? ?for (int i = 0; i < num; i++){
? ?delete[] female_rank_in_male[i];
? ?delete[] male_rank_in_female[i];
? ?}
? ?delete[] female_rank_in_male;
? ?delete[] male_rank_in_female;*/

? ?delete[] date_of_male;
? ?delete[] date_of_female;
? ?delete[] num_of_chased_female;

? ?return?0;
}

bool?finish_or_not(int?num,?int?*date_of_male){

? ?cout?<<?endl?<<?"排序:";
? ?for?(int?i =?0; i < num; i++){
? ? ? ?cout?<< date_of_male[i] <<?" ";
? ?}
? ?cout?<<?endl?<<?endl;

? ?for?(int?i =?0; i < num; i++){
? ? ? ?if?(date_of_male[i] ==?0){
? ? ? ? ? ?cout?<<?"還未完全匹配......"?<<?endl?<<?endl;
? ? ? ? ? ?return?false;
? ? ? ?}
? ?}
? ?cout?<<?"已完全匹配!!!"?<<?endl;
? ?return?true;
}

//比較某女人現在的約會對象和追求者哪個在她心目中排行更高
//注意數組參數是一維(一位女人的優先表而不是所有女人的優先表)
bool?current_male_is_better(int?num,?int?*male_rank_in_female,?int?current,?int?chasing){
? ?int?rank_of_current, rank_of_chasing;
? ?for?(int?i =?0; i < num; i++){
? ? ? ?if?(male_rank_in_female[i] == current){
? ? ? ? ? ?rank_of_current = i;
? ? ? ?}
? ? ? ?if?(male_rank_in_female[i] == chasing){
? ? ? ? ? ?rank_of_chasing = i;
? ? ? ?}
? ?}
? ?cout?<<?"現任排名是 "?<< rank_of_current <<?" 追求者排名是 "?<< rank_of_chasing <<?endl;
? ?if?(rank_of_current < rank_of_chasing)
? ? ? ?return?true;
? ?else
? ? ? ?return?false;
}


更新:新增萌萌噠版本,像寫小說一樣說出來~

****

#include?<iostream>
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?num =?5;

? ?int?female_rank_in_male[5][5] = { {?2,?1,?4,?5,?3?}, {?4,?2,?1,?3,?5?}, {?2,?5,?3,?4,?1?}, {?1,?4,?3,?2,?5?}, {?2,?4,?1,?5,?3?} };
? ?int?male_rank_in_female[5][5] = { {?5,?1,?2,?4,?3?}, {?3,?2,?4,?1,?5?}, {?2,?3,?4,?5,?1?}, {?1,?5,?4,?3,?2?}, {?4,?2,?5,?3,?1?} };

? ?for?(int?i =?0; i < num; i++){
? ? ? ?cout?<<?"男生 "?<< i <<?" 心目中妹子的排行:";
? ? ? ?for?(int?j =?0; j < num; j++){
? ? ? ? ? ?cout?<< female_rank_in_male[i][j] <<?" ";
? ? ? ?}
? ? ? ?cout?<<?endl;
? ?}
? ?cout?<<?endl;
? ?for?(int?i =?0; i < num; i++){
? ? ? ?cout?<<?"女生 "?<< i <<?" 心目中男生的排行:";
? ? ? ?for?(int?j =?0; j < num; j++){
? ? ? ? ? ?cout?<< male_rank_in_female[i][j] <<?" ";
? ? ? ?}
? ? ? ?cout?<<?endl;
? ?}

? ?//男生和妹子正在約會的對象
? ?int?*date_of_male =?new?int[num];
? ?int?*date_of_female =?new?int[num];
? ?for?(int?i =?0; i < num; i++){
? ? ? ?date_of_male[i] =?0;
? ? ? ?date_of_female[i] =?0;
? ?}

? ?//男生追求過的妹子的數量
? ?int?*num_of_chased_female =?new?int[num];
? ?for?(int?i =?0; i < num; i++){
? ? ? ?num_of_chased_female[i] =?0;
? ?}

? ?do{//如果有男生沒有對象
? ? ? ?for?(int?i =?0; i < num; i++){//按序號遍歷所有男生
? ? ? ? ? ?cout?<<?endl?<<?"正在查看男生 "?<< i +?1?<<?" 有沒有對象..."?<<?endl;
? ? ? ? ? ?if?(date_of_male[i] ==?0){//如果某男生沒有對象
? ? ? ? ? ? ? ?cout?<<?"男生 "?<< i +?1?<<?" 沒有對象!"?<<?endl;
? ? ? ? ? ? ? ?//該男生準備追的妹子(該男生優先表中還沒追求過的排名最高的妹子)
? ? ? ? ? ? ? ?int?female_to_chase = female_rank_in_male[i][num_of_chased_female[i]];
? ? ? ? ? ? ? ?//該男生準備追的妹子的現任
? ? ? ? ? ? ? ?int?date_of_female_to_chase = date_of_female[female_to_chase -?1];
? ? ? ? ? ? ? ?cout?<<?"男生 "?<< i +?1?<<?" 準備追妹子 "?<< female_to_chase;
? ? ? ? ? ? ? ?if?(date_of_female_to_chase !=?0){?
? ? ? ? ? ? ? ? ? ?cout?<<?",妹子現任是 "?<< date_of_female_to_chase <<?endl;?
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?else{
? ? ? ? ? ? ? ? ? ?cout?<<?",妹子目前是單身的呢~"?<<?endl;
? ? ? ? ? ? ? ?}

? ? ? ? ? ? ? ?if?(date_of_female_to_chase ==?0){//如果該男生準備追的妹子沒有現任
? ? ? ? ? ? ? ? ? ?date_of_male[i] = female_to_chase;//該男生的對象變成準備追的妹子
? ? ? ? ? ? ? ? ? ?date_of_female[female_to_chase -?1] = i +?1;//男生準備追的妹子的對象變成該男生
? ? ? ? ? ? ? ? ? ?cout?<<?"這樣子的話,男生 "?<< i +?1?<<?" 和妹子 "?<< female_to_chase <<?" 在一起了!"?<<?endl;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?else?if?(current_male_is_better(num, male_rank_in_female[female_to_chase -?1], date_of_female_to_chase, i +?1)){
? ? ? ? ? ? ? ? ? ?//如果該男生準備追的妹子的現任在妹子心目中比該男生更好,則什么也不做
? ? ? ? ? ? ? ? ? ?cout?<<?"所以!男生 "?<< i +?1?<<?" 被妹子 "?<< female_to_chase <<?" 拒絕了!"?<<?endl;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?else{//如果該男生比該男生準備追的妹子的現任在妹子心目中更好
? ? ? ? ? ? ? ? ? ?date_of_male[date_of_female_to_chase -?1] =?0;//該男生準備追的妹子的現任回到單身狀態
? ? ? ? ? ? ? ? ? ?date_of_male[i] = female_to_chase;//該男生的對象變成準備追的妹子
? ? ? ? ? ? ? ? ? ?date_of_female[female_to_chase -?1] = i +?1;//該男生準備追的妹子的對象變成該男生
? ? ? ? ? ? ? ? ? ?cout?<<?"所以!男生 "?<< i +?1?<<?" 和妹子 "?<< female_to_chase <<?" 在一起了!"?<<?endl;
? ? ? ? ? ? ? ? ? ?cout<<?"與此同時,不幸的是,男生 "?<< date_of_female_to_chase <<?" 變成單身狗了QAQ"?<<?endl;
? ? ? ? ? ? ? ?}

? ? ? ? ? ? ? ?num_of_chased_female[i]++;//該男生追過的妹子數量加1
? ? ? ? ? ?}
? ? ? ? ? ?else{
? ? ? ? ? ? ? ?cout?<<?"男生已經跟妹子 "?<< date_of_male[i] <<?" 在一起了呢~~下一個吧~~~"?<<?endl;
? ? ? ? ? ?}
? ? ? ?}
? ?}?while?(finish_or_not(num, date_of_male) ==?false);

? ?//最后的輸出
? ?cout?<<?endl?<<?"┌───故事的結局───┐"<<endl;
? ?cout?<<?"│ ? ? ? ? ? ? ? ? ? ? ?│"?<<?endl;
? ?for?(int?i =?0; i < num; i++){
? ? ? ?cout?<<?"│ ? ? 男生"?<< i +?1?<<?" - "<<?"女生"?<< date_of_male[i] <<" ? ?│"<<?endl;;
? ?}

? ?cout?<<?"│ ? ? ? ? ? ? ? ? ? ? ?│"?<<?endl;
? ?cout?<<?"└───────────┘"?<<?endl?<<?endl;
? ?delete[] date_of_male;
? ?delete[] date_of_female;
? ?delete[] num_of_chased_female;
? ?system("pause");
? ?return?0;
}

bool?finish_or_not(int?num,?int?*date_of_male){

? ?for?(int?i =?0; i < num; i++){
? ? ? ?if?(date_of_male[i] ==?0){
? ? ? ? ? ?cout?<<?"還有單身狗......再來~"?<<?endl;
? ? ? ? ? ?return?false;
? ? ? ?}
? ?}
? ?cout?<<?endl?<<?"已全部脫離單身狗行列!!!"?<<?endl;
? ?return?true;
}

//比較某妹子現在的對象和追求者哪個在她心目中排行更高
//注意數組參數是一維(一位妹子的優先表而不是所有妹子的優先表)
bool?current_male_is_better(int?num,?int?*male_rank_in_female,?int?current,?int?chasing){
? ?int?rank_of_current, rank_of_chasing;
? ?for?(int?i =?0; i < num; i++){
? ? ? ?if?(male_rank_in_female[i] == current){
? ? ? ? ? ?rank_of_current = i;
? ? ? ?}
? ? ? ?if?(male_rank_in_female[i] == chasing){
? ? ? ? ? ?rank_of_chasing = i;
? ? ? ?}
? ?}
? ?cout?<<?"在妹子心目中現任排名是 "?<< rank_of_current <<?" ,而追求者排名則是 "?<< rank_of_chasing <<?endl;
? ?if?(rank_of_current < rank_of_chasing)
? ? ? ?return?true;
? ?else
? ? ? ?return?false;
}


喜歡這個「愛情故事」么?


本文作者:CSDN博客作者-theusProme

文章版權歸原作者所有,轉載僅供學習使用,不用于任何商業用途,如有侵權請留言聯系刪除,感謝合作。


總結

以上是生活随笔為你收集整理的稳定匹配问题——稳定婚姻算法设计的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。