美文网首页
稳定匹配

稳定匹配

作者: 理性思考空船心态 | 来源:发表于2021-03-04 09:35 被阅读0次

    #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;

    相关文章

      网友评论

          本文标题:稳定匹配

          本文链接:https://www.haomeiwen.com/subject/mcbhqltx.html