#include<iostream>
#define MAXSIZE 20
using namespace std;
typedef struct
{
char name[20];
char sex;
}Person;
typedef struct
{
Person *base;
int front;
int rear;
}Queue;
void creatQueue(Queue &Q)
{
Q.base=new Person[MAXSIZE];
Q.front=Q.rear=0;
return;
}
bool inQueue(Queue &Q,Person e)
{
if((Q.rear+1)%MAXSIZE==Q.front)
return false;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXSIZE;
return true;
}
int outQueue(Queue &Q,Person &e)
{
if(Q.front==Q.rear)
return 0;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXSIZE;
return 1;
}
int QueueEmpty(Queue Q)
{
if(Q.front==Q.rear)
return 1;
return 0;
}
Person GetHead(Queue Q)
{
if(Q.front!=Q.rear)
return Q.base[Q.front];
}
void DancePartner(Person dancer[],int num)
{
Queue Mdancers;
Queue Fdancers;
Person p;
creatQueue(Mdancers);
creatQueue(Fdancers);
for(int i=0;i<num;i++)
{
p=dancer[i];
if(p.sex=='F')
inQueue(Fdancers,p);
else inQueue(Mdancers,p);
}
cout<<"跳舞的男女生组合:"<<endl;
while(!QueueEmpty(Fdancers)&&!QueueEmpty(Mdancers))
{
outQueue(Fdancers,p);
cout<<"跳舞组合中女生和男生名字分别为:";
cout<<p.name<<" ";
outQueue(Mdancers,p);
cout<<p.name<<endl;
}
if(!QueueEmpty(Fdancers))
{
p=GetHead(Fdancers);
cout<<"第一个需要获得同伴的女生是:"<<p.name<<endl;
}
if(!QueueEmpty(Mdancers))
{
p=GetHead(Mdancers);
cout<<"第一个需要获得同伴的男生是:"<<p.name<<endl;
}
return;
}
int main()
{
int n;
cout<<"请输入跳舞的人数:";
cin>>n;
Person dancer[n];
cout<<"请输入跳舞人的信息:"<<endl;
for(int i=0;i<n;i++)
{
cin.get();
cout<<"第"<<i+1<<"个人的姓名:";
cin>>dancer[i].name;
cout<<"第"<<i+1<<"个人的性别(F/M):";
cin>>dancer[i].sex;
}
DancePartner(dancer,n);
return 0;
}
该代码采用的是C++进行编译,进行队列实用问题中舞伴问题的操作。
不懂得可以私信或者评论区留言,一起进步吧!
|