其他分享
首页 > 其他分享> > GS同桌匹配(改良版本)

GS同桌匹配(改良版本)

作者:互联网

#include <bits/stdc++.h>
using namespace std;
bool check1(int, int *);
bool find1(int num, int *malerank, int current, int chasing);
bool check2(int, int *);
bool find2(int num, int *malerank, int current, int chasing);
void main1(){
   int num ;
   int femalerank[505][505] ;
   int malerank[505][505] ;

   cout<<"请输入总人数(男女相等,请输入偶数):"<<endl;
   cin>>num;
   num/=2; 

   for (int i = 0; i < num; i++){
       cout << "男生 " << i +1<< "理想中的女同桌(按意愿强度依次输入序号)";
       for (int j = 0; j < num; j++){
           cin >> femalerank[i][j] ;
       }
       cout << endl;
   }
   cout << endl;
   for (int i = 0; i < num; i++){
       cout << "女生 " << i +1<< "理想中的男同桌(按意愿强度依次输入序号)";
       for (int j = 0; j < num; j++){
           cin >> malerank[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 = femalerank[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 (find1(num, malerank[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 << " 没有同桌了" << endl;
               }

               num_of_chased_female[i]++;//该男生追过的女生数量加1
           }
           else{
            //   cout << "男生已经跟女生 " << date_of_male[i] << " 在一起了,继续尝试" << endl;
           }
       }
   } while (check1(num, date_of_male) == false);

   //最后的输出
   cout << endl << "┌───────最佳的组合───────┐"<<endl;
   cout << "│                        │" << endl;
   for (int i = 0; i < num; i++){
       cout << "│     男生" <<setw(2)<< i + 1 << " - "<< "女生" <<setw(2)<< 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 ;
}

void main2(){
   int num ;
   int femalerank[505][505] ;
   int malerank[505][505] ;

   cout<<"请输入总人数(男女相等,请输入偶数):"<<endl;
   cin>>num;
   num/=2; 

   for (int i = 0; i < num; i++){
       cout << "男生 " << i +1<< "理想中的女同桌(按意愿强度依次输入序号)";
       for (int j = 0; j < num; j++){
           cin >> femalerank[i][j] ;
       }
       cout << endl;
   }
   cout << endl;
   for (int i = 0; i < num; i++){
       cout << "女生 " << i +1<< "理想中的男同桌(按意愿强度依次输入序号)";
       for (int j = 0; j < num; j++){
           cin >> malerank[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 = femalerank[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 (find2(num, malerank[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 << " 没有同桌了" << endl;
               }

               num_of_chased_female[i]++;//该男生追过的女生数量加1
           }
           else{
               cout << "男生已经跟女生 " << date_of_male[i] << " 坐在一起了,继续尝试" << endl;
           }
       }
   } while (check2(num, date_of_male) == false);

   //最后的输出
   cout << endl << "┌───────最佳的组合───────┐"<<endl;
   cout << "│                        │" << endl;
   for (int i = 0; i < num; i++){
       cout << "│     男生" <<setw(2)<< i + 1 << " - "<< "女生" <<setw(2)<< 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 ;
}

bool check1(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 find1(int num, int *malerank, int current, int chasing){
   int rank_of_current, rank_of_chasing;
   for (int i = 0; i < num; i++){
       if (malerank[i] == current){
           rank_of_current = i;
       }
       if (malerank[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;
}

bool check2(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 find2(int num, int *malerank, int current, int chasing){
   int rank_of_current, rank_of_chasing;
   for (int i = 0; i < num; i++){
       if (malerank[i] == current){
           rank_of_current = i;
       }
       if (malerank[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;
}

int main(){
    int c;
    cout<<"制作者杨江楠"<<endl; 
    cout<<"是否需要显示过程"<<endl<<"是请输入0否请输入1"<<endl;
    cin>>c;
    if(c==1) main1();
    else main2();
    return 0; 
}
/*
10
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
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
*/ 

采用GS算法实现同桌匹配,在参考了部分博主的程序之后增加了几项主要改良

  1. 在输入时可以选择是否需要输出过程,避免部分大数据产生的冗长过程影响应用效率
  2. 输入时增加部分引导与解释,便于使用
  3. 输出时增加场宽设定,并重新对齐,使结果简单明确

 

标签:匹配,cout,int,同桌,num,bool,GS,505,malerank
来源: https://blog.csdn.net/yangyong1973/article/details/123309837