|
发表于 2021-10-23 00:50:04
|
显示全部楼层
这怎么可能是完整的题目?
我要的是这个,你的题目不完整,是要别人去百度找你的问题的题目吗?
下面这个题目我找了挺久的,你自己贴出来不是更好?
- 问题叙述
- 假设在周末舞会上,男士们和女士们进入舞厅时,各自排成一队。跳舞开始时,依次从男队和女队的队头上各出一人配成舞伴。若两队初始人数不相同,则较长的那一队中未配对者等待下一轮舞曲。现要求写一算法模拟上述舞伴配对问题。
- 【问题分析】
- 先入队的男士或女士亦先出队配成舞伴。因此该问题具体有典型的先进先出特性,可用队列作为算法的数据结构。
- 在算法中,假设男士和女士的记录存放在一个数组中作为输入,然后依次扫描该数组的各元素,并根据性别来决定是进入男队还是女队。当这两个队列构造完成之后,依次将两队当前的队头元素出队来配成舞伴,直至某队列变空为止。此时,若某队仍有等待配对者,算法输出配对的结果和此队列中等待者的人数及排在队头的等待者的名字,他(或她)将是下一轮舞曲开始时第一个可获得舞伴的人。
复制代码
- #include <fstream>
- #include <iostream>
- #include <string>
- #define MAXQSIZE 100 //队列可能达到的最大长度
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- using namespace std;
- typedef struct {
- char name[20]; //姓名
- char sex; //性别,'F'表示女性,'M'表示男性
- } Person;
- //- - - - - 队列的顺序存储结构- - - - -
- typedef struct {
- Person *base; //队列中数据元素类型为Person
- int front; //头指针
- int rear; //尾指针
- int qsize;
- } SqQueue;
- // 你程序中没有使用这两个
- //SqQueue Mdancers, Fdancers; //分别存放男士和女士入队者队列
- int InitQueue(SqQueue &Q) { //构造一个空队列Q
- Q.base = new Person[MAXQSIZE]; //为队列分配一个最大容量为MAXSIZE的数组空间
- if(!Q.base)
- exit(OVERFLOW); //存储分配失败
- Q.front = Q.rear = 0; //头指针和尾指针置为零,队列为空
- Q.qsize = 0;
- return OK;
- }
- // 释放函数一定一定一定一定一定要写
- void queue_deinit(SqQueue &q) {
- delete [] q.base;
- }
- int EnQueue(SqQueue &Q, Person e) { //插入元素e为Q的新的队尾元素
- if((Q.rear + 1) % MAXQSIZE ==
- Q.front) //尾指针在循环意义上加1后等于头指针,表明队满
- return ERROR;
- Q.base[Q.rear] = e; //新元素插入队尾
- Q.rear = (Q.rear + 1) % MAXQSIZE; //队尾指针加1
- Q.qsize++;
- return OK;
- }
- int QueueEmpty(SqQueue &Q) {
- if(Q.front == Q.rear) //队空
- return OK;
- else
- return ERROR;
- }
- int DeQueue(SqQueue &Q, Person &e) //删除Q的队头元素,用e返回其值
- {
- if(Q.front == Q.rear)
- return ERROR; //队空
- e = Q.base[Q.front]; //保存队头元素
- Q.front = (Q.front + 1) % MAXQSIZE; //队头指针加1
- return OK;
- }
- Person GetHead(SqQueue Q) { //返回Q的队头元素,不修改队头指针
- if(Q.front != Q.rear) //队列非空
- return Q.base[Q.front]; //返回队头元素的值,队头指针不变
- // 非空的时侯返回 Q.base[Q.front]
- // 空的时侯怎么办?
- // 你没写,我也不知道对于你的这个设计应该返回什么,那就直接退出吧
- exit(ERROR);
- }
- //算法3.23 舞伴问题填写完整
- void DancePartner(Person dancer[],
- int num) { //结构数组dancer中存放跳舞的男女,num是跳舞的人数。
- //当一个队列空,另一个不空时,输出队头等待者的名字
- SqQueue Man, Woman;
- Person p;
- InitQueue(Man);
- InitQueue(Woman);
- for(int i = 0; i < num; i++) {
- p = dancer[i];
- if(p.sex == 'M')
- EnQueue(Man, p);
- else
- EnQueue(Woman, p);
- }
- cout << "请输入几首歌:" << endl;
- int n;
- int j = 1;
- cin >> n;
- // cout<<"本次跳舞的组合是:"<<endl;
- cout << "女士在前,男士在后。。。" << endl;
- /*
- while(n--) {
- cout << "本次是第" << j++ << "次:" << endl;
- while(!QueueEmpty(Man)) {
- // 队列空的时侯出对?
- if(QueueEmpty(Woman))
- DeQueue(Woman, p);
- DeQueue(Man, p);
- cout << p.name << " ";
- DeQueue(Woman, p);
- cout << p.name << endl;
- }
- // 不要在 queue函数的外面使用 queue 函数的内部数据结构
- Man.front = (Man.front + 1) % Man.qsize;
- if(QueueEmpty(Woman))
- DeQueue(Woman, p);
- p = GetHead(Woman);
- printf("第一个出场的未配对者的姓名:%s\n", p.name);
- }
- */
- SqQueue a, b;
- InitQueue(a);
- InitQueue(b);
- while(n--) {
- cout << "本次是第" << j++ << "次:" << endl;
- while(!QueueEmpty(Man) && !QueueEmpty(Woman)) {
- DeQueue(Man, p);
- EnQueue(a, p);
- cout << p.name << " ";
- DeQueue(Woman, p);
- EnQueue(b, p);
- cout << p.name << endl;
- }
- SqQueue *remain = !QueueEmpty(Man) ? &Man : &Woman;
- p = GetHead(*remain);
- printf("第一个出场的未配对者的姓名:%s\n", p.name);
- while(!QueueEmpty(a)) {
- DeQueue(a, p);
- EnQueue(Man, p);
- }
- while(!QueueEmpty(b)) {
- DeQueue(b, p);
- EnQueue(Woman, p);
- }
- }
- queue_deinit(Man);
- queue_deinit(Woman);
- queue_deinit(a);
- queue_deinit(b);
- }
- Person dancer[MAXQSIZE];
- int main() {
- int i = 0;
- fstream file;
- file.open("DancePartner.txt");
- if(!file) {
- cout << "错误!未找到文件!\n\n" << endl;
- exit(ERROR);
- }
- while(!file.eof()) {
- file >> dancer[i].name >> dancer[i].sex;
- i++;
- }
- file.close(); // 关闭一下最好
- DancePartner(dancer, i - 1);
- return 0;
- }
复制代码 |
|