SHAOXIAOJ正在加载中...

1653: 队列-舞伴配对问题

金币值:2 定数:8 时间限制:1.000 s 内存限制:128 M
正确:7 提交:10 正确率:70.00% 命题人:
点赞量:0 收藏量:0 题目类型:程序 知识点: 数据结构-栈与队列

题目描述

有一群青年男女陆续来到舞厅,根据到达舞厅的先后顺序进行男女配对,输出配对成功的每对男女姓名。

输入格式

第1行 输入一个整数n,表示有n个青年人先后来到舞厅。
第2-n+1行,每行输入一个青年人的姓名和性别(m:表示男,f:表示女)。

输出格式

输出配对成功的舞伴姓名,每行输出一对舞伴姓名,姓名之间用空格隔开。

输入样例    复制

5
aaaa m
bbbb m
cccc f
dddd f
eeee m

输出样例    复制

aaaa cccc
bbbb dddd

提示

#include <stdio.h>
#include <stdlib.h>
 
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100 
typedef int Status;

typedef struct{
	char name[20];
	char sex;
}QElemType;

typedef struct {
    QElemType *base;
    int front; 
    int rear;
} SqQueue;

void InitQueue(SqQueue &Q);

void EnQueue(SqQueue &Q,QElemType e);

void DeQueue(SqQueue &Q,QElemType &e);

void DanceParter(SqQueue Q1,SqQueue Q2);

int QueueEmpty(SqQueue Q);

int main(void) {
    SqQueue Q1,Q2;
    InitQueue(Q1);
    InitQueue(Q2);
    
    int n;
    scanf("%d",&n);	
    QElemType person;
    for(int i=1;i<=n;i++){    
    	scanf("%s %c",person.name,&person.sex);
		if (person.sex=='m'){
			EnQueue(Q1,person);
		}
		else{
			EnQueue(Q2,person);
		} 	
    }
		
    DanceParter(Q1,Q2);
    
    return 0;
}

void InitQueue(SqQueue &Q){
	Q.base=new QElemType[MAXSIZE];
	Q.front=0;
	Q.rear=0;	 
}

void EnQueue(SqQueue &Q,QElemType e){
	Q.base[Q.rear]=e;
	Q.rear=(Q.rear+1)%MAXSIZE;
}

void DeQueue(SqQueue &Q,QElemType &e){
	e=Q.base[Q.front];
	Q.front=(Q.front+1)%MAXSIZE;
}

int QueueEmpty(SqQueue Q){
	if(Q.rear==Q.front){
		return 1;
	}
	else{
		return 0;
	}
}

void DanceParter(SqQueue Q1,SqQueue Q2){
	
}

//如果使用C++提供的queue实现青年男女配对问题,
//其中入队列,取队列头元素,出队列,队列判空等成员函数系统已经实现,
//完善以下代码:

#include <stdio.h>
#include <queue>
using namespace std;
typedef struct {
	char name[20];
	char sex;
} QElemType;
void DanceParter(queue<QElemType> Q1,queue<QElemType> Q2);
int main(void) {
	queue<QElemType> Q1,Q2;
	int n;
	scanf("%d",&n);
	QElemType person;
	for(int i=1; i<=n; i++) {
		scanf("%s %c",&person.name,&person.sex);
		if (person.sex=='m') {
			Q1.push(person);
		} else {
			Q2.push(person);
		}
	}
	DanceParter(Q1,Q2);
	return 0;
}
void DanceParter(queue<QElemType> Q1,queue<QElemType> Q2) {
	while(!Q1.empty() && !Q2.empty()) {
		QElemType man,woman;
		____________________
		printf("%s %s\n",man.name,woman.name);
	}
}