稳定匹配

#include <stdio.h>

#include <string.h>

#define person 4

#define true 1

#define false 0

// predilecations

char *maleTable[4][4][2] = {

{{"l", 0},{"l",0}, {"m", 0}, {"h", 0}},

{{"m", 0},{"m",0}, {"l", 0}, {"h", 0}},

{{"h", 0},{"h",0}, {"l", 0}, {"m", 0}},

{{"l", 0},{"m",0}, {"h", 0}, {"m", 0}}

};

char *femaleT[4][2] = {"mmlh","lmll","hhml","mlhh"};

int couple[4][4]={0};

int satisfied(int man)

{

int proposed = true; // proposed to all women

int isFree = true; // is free

for (int j = 0; j < 3; j++){

  // if the man is free, continue to

  // check if he hasn"t proposed to all women

  if (couple[man][j] != false){

  isFree = false;

  break;

  }

  if (maleTable[man][j][1] == false) {

  proposed = false;

  break;

  }

}

if ( isFree && !proposed) return false;

else return true;

}

int main(int argc, char *argv[])

{

if( argc == 1) printf("Default number of man is 4\n");

else if (argc == 2) printf("Number of man is %d\n", (int)*argv[1]);

else fprintf(stderr, "Usage: ./CG integer \n");

int i, j = 0;

nextMan: if ( i < 3) i++;

  else return 0; // end if all men are checked

  // the man isn"t free or has proposed to all women

  if (satisfied(j)) goto nextMan;

  else printf("Continue\n");

  // find the women has highest favor

  //

  int woman = 1;

  int man = 0;

  for (; man < 3; man++){

  printf("man is %d", man);

  // no need to check this man

  if ( man == i) continue;

  // the woman isn't free

  if ( couple[man][woman]) break;

  // the woman is free

  if( man == 3) {

    couple[i][woman] = 1;

    goto nextMan;

  }

  }

  printf("Compare\n");

return 0;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容