一个一班有m个女生,有n个男生(m不等于n),现要开一个舞会. 男女生分别编号坐在舞池的两边的椅子上.每曲开始时,依次从男生和女生中各出一人配对跳舞, 本曲没成功配对者坐着等待下一曲找舞伴.
请设计一系统模拟动态地显示出上述过程,要求如下:
1)输出每曲配对情况
2)计算出任何一个男生(编号为X)和任意女生(编号为Y),在第K曲配对跳舞的情况.至少求出K的两个值
请问有人帮我解释一下这个问题吗
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
threenewbee 2023-05-29 20:26关注#include<iostream> using namespace std; template <class T> class cirularQueue //定义一个一个循环队列 { private: int MaxSize; int first; //头指针 int last; //尾指针 T *data; public: cirularQueue(int MaxLength) { MaxSize=MaxLength; first=last=0; data=new T[MaxLength]; } ~cirularQueue() //定义析构函数,使对象在撤销时释放 { first=last=0; delete []data; } void Initqueue() //队列的声明 { for(int i=0;i<MaxSize-1;i++) push(i); } bool IsFull() //判断队列是否已满 { if((last+1)%MaxSize==first) return true; else return false; } bool IsEmpty() //判断队列是否为空 { if(first==last) return true; else return false; } void push(T info) //入队 { if(IsFull()) { cout<<"错误!队列已满!"<<endl; } else { data[last]=info; last=(last+1)%MaxSize; } } void Pop(T &info) //出队 { if(IsEmpty()) { cout<<"错误!队列为空!"<<endl; } else { info=data[first]; first=(first+1)%MaxSize; } } void GetHead(T &info) //取队首元素 { if(IsEmpty()) { cout<<"错误!队列为空!"<<endl; } else { info=data[first]; } } }; void Initqueue(cirularQueue<int>&,int); void display(int,int); void charge(int,int); using namespace std; static int songnum=0; //定义歌曲的数量并初始化为0 static int m=0,n=0; //男生和女生的人数 int main() //主函数 { cout<<"请分别输入男生和女生的人数:"; cin>>m>>n; display(m,n); int a=0,b=0; //男生和女生的编号,以判断他们在第几首歌时能在一起跳舞 char quit='y'; //判断是否继续输入,如果继续输入,则输入'y';否则输入'n' while(quit!='n') { cout<<"请输入男生和女生的编号:"; cin>>a>>b; while((a>m)||(b>n)) //如果输入错误 { cout<<"输入的编号过大,请重新输入:"; cin>>a>>b; } charge(a,b); cout<<"是否继续(是请输入'y',否则请输入'n'):"; cin>>quit; } return 0; } void Initqueue(cirularQueue<int> &Q,int m) //初始化队列 { for(int i=1;i<=m;i++) Q.push(i); } void display(int m,int n) { cirularQueue<int> man(m+1); cirularQueue<int> woman(n+1); Initqueue(man,m); Initqueue(woman,n); cout<<"请输入曲目数:"; cin>>songnum; cout<<"每曲的配对情况为:"<<endl; for(int k=1;k<=songnum;k++) { int x=0,y=0; //男生和女生的编号 man.Pop(x); //男生按顺序出对跳舞 woman.Pop(y); //女生按顺序出对跳舞 cout<<"第"<<k<<"曲:\t"<<x<<"号男生<->"<<y<<"号女生"<<endl; //他们在一起跳舞 man.push(x); //跳完舞后男生再次进入队列等在下一次跳舞 woman.push(y); //跳完舞后男生再次进入队列等在下一次跳舞 } } void charge(int a,int b) { int count=0; //定义舞曲计数以记录他们能在第几曲时在一起跳舞 cirularQueue<int> man1(m+1); cirularQueue<int> woman1(n+1); Initqueue(man1,m); Initqueue(woman1,n); while(count<=songnum) { int x, y; count++; man1.Pop(x); woman1.Pop(y); man1.push(x); woman1.push(y); if((x==a)&&(y==b)) { cout<<"第"<<count<<"首曲:\t"<<a<<"号男生<->"<<b<<"号女生"<<endl; break; } } //如果他们在这个舞会上不能在一起跳舞,则输出 if(count==songnum+1) cout<<"他们在这个舞会上不可能在一起跳舞"<<endl; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报