代码实现
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 100
typedef struct
{
int wTotal;
char wName;
int WR;
char *mRank;
int hus;
int husInMen;
}*WOMEN,EWOMEN;
typedef struct
{
int mTotal;
char mName;
int MR;
char *wRank;
int times;
int amount;
}*MEN,EMEN;
void Create(WOMEN &W,MEN &M)
{
printf("请输入女生人数:");
scanf("%d",&W[0].wTotal);
printf("请输入男生人数:");
scanf("%d",&M[0].mTotal);
for(int i = 1;i<=W[0].wTotal;i++)
{
W[i].mRank = (char *)malloc((MAX+1)*sizeof(char));
W[i].hus = 0;
W[i].husInMen = 0;
W[i].WR = 0;
}
for (int n = 1; n <= W[0].wTotal; n++)
{
printf("请输入%d号女生的名字:",n);
scanf("
%c",&W[n].wName);
}
for (int j = 1; j <= W[0].wTotal; j++)
{
printf("请输入%c对男生的排序:",W[j].wName);
for(int m = 0;m < M[0].mTotal;m++)
{
scanf("
%c",&W[j].mRank[m]);
}
}
M[0].amount = 0;
for(j = 1; j <= M[0].mTotal; j++)
{
M[j].wRank = (char *)malloc((MAX+1)*sizeof(char));
M[j].MR = 0;
M[j].times = 0;
}
for(int t = 1; t<=M[0].mTotal;t++)
{
printf("请输入%d号男生的名字:",t);
scanf("
%c",&M[t].mName);
}
for(j = 1; j <= M[0].mTotal; j++)
{
printf("请输入%c对女生的排序:",M[j].mName);
for(int m = 0; m<W[0].wTotal;m++)
{
scanf("
%c",&M[j].wRank[m]);
}
}
}
void Proposal(MEN M,WOMEN W)
{
int i=1;
int temp,now;
int wifeLoc;
while(M[0].amount<M[0].mTotal)
{
if(i>M[0].mTotal)
{
i = i%M[0].mTotal;
}
while(M[i].MR == 0)
{
char ch=M[i].mName;
int location=M[i].times;
char wifeName=M[i].wRank[location];
for (int j = 1; j <= W[0].wTotal; j++)
{
if (W[j].wName == wifeName)
{
wifeLoc=j;
break;
}
}
for (int t = 0;t<M[0].mTotal;t++)
{
if (W[wifeLoc].mRank[t]==ch)
{
temp = t;
break;
}
}
if (W[wifeLoc].WR == 0)
{
M[i].MR = 1;
(M[i].times)++;
(M[0].amount)++;
W[wifeLoc].WR = 1;
W[wifeLoc].husInMen=i;
W[wifeLoc].hus=temp;
}
else
{
now=W[wifeLoc].hus;
if(now<temp)
{
(M[i].times)++;
}
else
{
int nowHus = W[wifeLoc].husInMen;
M[nowHus].MR = 0;
(M[i].times)++;
M[i].MR = 1;
W[wifeLoc].husInMen=i;
W[wifeLoc].hus = temp;
}
}
}
i++;
}
}
int main()
{
WOMEN W = (WOMEN)malloc((MAX+1)*sizeof(EWOMEN));
MEN M = (MEN)malloc((MAX+1)*sizeof(EMEN));
printf("**************注意:请保持男生与女生的人数相等!!!***********
");
Create(W,M);
Proposal(M,W);
for (int t = 1;t <= W[0].wTotal;t++)
{
int m = W[t].husInMen;
printf("%c<-->%c
",W[t].wName,M[m].mName);
}
system("pause");
return 0;
}
例子
结果
结果分析
证明结果是稳定的
假设Xavier和Bertha是不稳定的
按照算法逻辑推理,他们之间有两种关系:
一、
Xavier没跟Bertha进行过约会→Xavier喜欢他的女朋友胜过喜欢Bertha→Xavier和Bertha是稳定的
二、
Xavier于Bertha进行过约会:
(1)Xavier与Bertha互相喜欢→Xavier与Bertha是稳定的
(2)Bertha拒绝了Xavier→Bertha喜欢她的男朋友胜过喜欢Xavier→Xavier和Bertha是稳定的
综上所述,假设不成立,所以可证明,结果稳定
联系我
博客园:https://www.cnblogs.com/AWSG-Shaodw/
CSDN:https://blog.csdn.net/AngleWithShotgun/
简书:https://www.jianshu.com/u/df7323cbc116
微信公众号: