⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a.cpp

📁 是一个舞伴问题的解决算法
💻 CPP
字号:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct tagPerson
{
     char strName[40];
     tagPerson* pNext;
};

void main()
{
     tagPerson* pMan = new tagPerson;
     tagPerson* pFemale = new tagPerson;
     char cArr[100][30] = {0};
     bool cLoop = true;
     int nIndex = 0;
     int nCountM = 0, nCountF = 0;
     cout << "请输入今天到场的人员名单:" << endl;
///////////////////////////////////////////////////////   第1部分,输入全部人员名单      
     while(cLoop)//这样就完成了对全部人员姓名和性别的输入,为了方便,设置1为男,0为女
     {
           cout << "姓名:";
           cin >> cArr[nIndex];
           cout << "性别:";
           cin >> cArr[nIndex + 1];

           if(!strcmp(cArr[nIndex + 1],"1"))
           {
                 nCountM ++;
           }
           else if(!cArr[nIndex + 1] == 0)
           {
                 nCountF ++;
           }

           cout << "\n" <<"继续吗?(Y/N)" << endl;
           char cChoice = NULL;
           cin >> cChoice;
           
           if(cChoice == 'N' || cChoice == 'n')
           {
                 cLoop = false;
           }
           nIndex += 2;
     }

     int nCount = nCountM;
     
     if(nCountM > nCountF)
     {
           nCount = nCountF;
     }

     int nIndex1 = 0;
     tagPerson* pManHead = new tagPerson;
     pManHead->pNext = NULL;
     for(nIndex = 0; nIndex < nCountM + nCountF; )//把男人都挑出来,组成一个链表
     {
     
           if(!strcmp(cArr[nIndex + 1],"1"))//找到男人,将他们挑出来放在一个链表中
           {
                 tagPerson* pNewNode = new tagPerson;
                 pNewNode->pNext = NULL;
                 tagPerson* pTmp = pManHead;
                 strcpy(pNewNode->strName,cArr[nIndex]);
                 strcpy(pTmp->strName,pNewNode->strName);
                 while(NULL != pTmp->pNext)
                 {
                       pTmp = pTmp->pNext;
                 }
                 pTmp->pNext = pNewNode;
           
                 nIndex1 ++;
                 if(nIndex1 == 1)
                 {
                       pManHead = pNewNode;
                 }
           }
           nIndex += 2;
     }

     tagPerson* pFemaleHead = new tagPerson;
     pFemaleHead->pNext = NULL;
     for(nIndex = 0, nIndex1 = 0; nIndex < nCountM + nCountF; )//把女人都挑出来组成一个链表
     {

           if(!strcmp(cArr[nIndex + 1],"0"))//找到女人,放在链表中
           {
                 tagPerson* pNewNode = new tagPerson;
                 pNewNode->pNext = NULL;
                 tagPerson* pTmp = pFemaleHead;
                 strcpy(pNewNode->strName,cArr[nIndex]);
                 strcpy(pTmp->strName,pNewNode->strName);
                 while(NULL != pTmp->pNext)
                 {
                       pTmp = pTmp->pNext;
                 }
                 pTmp->pNext = pNewNode;

                 nIndex1 ++;
           }
           nIndex += 2;
     }
     
     while(!cLoop)
     {

           for(int nIndex3 = 1; nIndex3 < 11; nIndex3 ++)//第几首歌德循环
           {      
                 tagPerson* p1 = pManHead;

                 tagPerson* p2 = pFemaleHead;

                 cout << "第"<<nIndex3<<"首舞曲的名单如下:" << endl;//打印名单
                 for(int nIndex2 = 0; nIndex2 < (nCountM>nCountF?nCountF:nCountM); nIndex2 ++)
                 {
                       cout <<"配对的人的姓名为: 男士:" <<pManHead->strName << "\t"<< "女士:" << pFemaleHead->strName << endl;
                       tagPerson* pNewNode1 = new tagPerson;
                       pNewNode1->pNext = NULL;
                       strcpy(pNewNode1->strName,pManHead->strName);
                       p1->pNext = pNewNode1;//建立一个p1保存出列的男人名单
                       pManHead = pManHead->pNext;//把男人的队列头指针往后移动
                 
                       tagPerson* pNewNode2 = new tagPerson;
                       pNewNode2->pNext = NULL;
                       strcpy(pNewNode2->strName,pFemaleHead->strName);
                       p2->pNext = pNewNode2;//建立一个p2保存女人出列的名单
                       pFemaleHead = pFemale->pNext;//把女人的队列头指针往后移动
                       
                 }
                 pManHead->pNext = p1;//一轮舞曲之后,把p1接在男人的后面
                 pFemaleHead->pNext = p2;//把p2接在女人后面
                 system("pause");
           }
           cLoop = true;//到10首歌全部跳完后跳出循环
     }

     return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -