1#include <iostream>2 using namespacestd;3 intMain () {4 intSmallest_numberfree =0;//record single men with the lowest number5 inti = Smallest_numberfree, n, k =-1;//N represents how many pairs of men and women6 7cout <<"Please output how many pairs of men and women:"; 8CIN >>N;9 int**man =New int*[n], **woman =New int*[n];//The former represents the ranking of females in each male's heart, the latter representing the rank of men in mindTen int*m_isfree =New int[n], *w_isdating=New int[n];//The former-1 stands for single, is free, the latter-1 represents a date that has not been dated, the value of greater than 1 is the object of the appointment One int*m_bestwoman =New int[n];//representing the highest ranking of women in every man's current non-confession. A int*count =New int[n];//representing how many times each man has confessed. -cout <<"Please output male/female number from 0 to"<< N-1<<"the various minds of female/male number 0 to"<< N-1<<"rankings (spaces):"<<Endl; - for(inti =0; I < n; i++){ theMan[i] =New int[n]; Woman[i] =New int[n]; -cout <<"male"<< I <<"Number:"; - for(intj =0; J < N; J + +){ -CIN >>Man[i][j]; +M_ISFREE[J] =-1; -W_ISDATING[J] =-1; +Count[i] =-1; A } atM_bestwoman[i] = man[i][0]; -cout <<"female"<< I <<"Number:"; - for(intj =0; J < N; J + +) -CIN >>Woman[i][j]; - } - while(Smallest_numberfree < N) {//If a man is still single ini =Smallest_numberfree; - if(W_isdating[m_bestwoman[i]] = =-1){//If a man does not confess the most admired by a woman who has not dated toM_isfree[i] = M_bestwoman[i];//A man started dating her, good times. +W_isdating[m_bestwoman[i]] = i;//woman records the date object -M_bestwoman[i] = man[i][++count[i]];//The most admired among women who have not yet confessed their records. thesmallest_numberfree++; * } $ Else{Panax Notoginsengk = W_isdating[m_bestwoman[i]];//If a man sees a woman who has already started dating, K represents the ID of the person who is dating. - for(intj =0; J < N; J + +) {//The circle finds the woman's current date and the man who confessed the ranking among the women in the heart the if(Woman[m_bestwoman[i]][j] = = k) {//If a woman is on a date and the current object is better than the man who confessed. +M_bestwoman[i] = man[i][++count[i]];//The man who confessed started looking for the next AK =-1;//ID Clear 0 the Break; + } - if(Woman[m_bestwoman[i]][j] = = i) {//If a woman is on a date and has a better opinion of the speaker than the current date. $M_ISFREE[K] =-1;//The man who's going to be left alone. $M_bestwoman[k] = man[k][++Count[k]]; -M_bestwoman[i] = man[i][++count[i]];//The successful and the abandoned are reserved for the next most admired woman - Break; the } - }Wuyi } the if(k! =-1){//indicates that the man is currently single -Smallest_numberfree =K; Wu } - } Aboutcout <<"The final generated stable match is:"<<Endl; $cout <<"male"<<"\ t"<<"female"<<"\ t"<<Endl; - for(inti =0; I < n; i++){ -cout << i <<"\ t"<< M_isfree[i] <<"\ t"<<Endl; - } A for(inti =0; I < n; i++){ + delete[] man[i]; the Delete[]woman[i]; - } $ delete[] w_isdating; the delete[] M_bestwoman; the delete[] M_isfree; the delete[] count; the}
g_s man and woman matching algorithm (first program of algorithm 2016.09.19)