找舞伴
来源:互联网 发布:首批网络空间安全学院 编辑:程序博客网 时间:2024/04/29 05:38
Main.cpp
#include<stdio.h>
#include"cirqueue.h"#include"pairing.h"
int main(void)
{
int i,num;
scanf("%d",&num);
getchar();
cirqueue q;
for(i=0;i<num;i++)
{
gets((q.person[i]).name);
//getchar();
(q.person[i]).sex=getchar();
getchar();
}
dancepartners(q.person,num);
return 0;
}
cirqueue.h
#ifndef cirqueue_h
#define cirqueue_h
#define queuesize 100
typedef struct
{
char name[20];
char sex;
}dancer;
typedef struct
{
dancer person[queuesize];
int front;
int rear;
int count;
}cirqueue;
void init(cirqueue *q);
int is_empty(cirqueue *q);
int is_full(cirqueue *q);
void enqueue(cirqueue *q,dancer p);
dancer dequeue(cirqueue *q);
#endif
cirqueue.cpp
#include<stdio.h>
#include<stdlib.h>
#include"cirqueue.h"
void init(cirqueue *q)
{
q->front=q->rear=0;
q->count=0;
}
int is_empty(cirqueue *q)
{
return q->count==0;
}
int is_full(cirqueue *q)
{
return q->count==queuesize;
}
void enqueue(cirqueue *q,dancer p)
{
if(is_full(q))
{
printf("overflow!\n");
exit(EXIT_FAILURE);
}
q->count++;
q->person[q->rear]=p;
q->rear=(q->rear+1)%queuesize;
}
dancer dequeue(cirqueue *q)
{
dancer x;
if(is_empty(q))
{
printf("underflow!\n");
exit(EXIT_FAILURE);
}
q->count--;
x=q->person[q->front];
q->front=(q->front+1)%queuesize;
return x;
}
pairing.h
#ifndef pairing_h
#define pairing_h
#include"cirqueue.h"
void dancepartners(dancer person[],int i);
#endif
pairing.cpp
#include<stdio.h>
#include"cirqueue.h"
void dancepartners(dancer person[],int num)
{
int i;
dancer p;
cirqueue MQueue,FQueue;
init(&MQueue);
init(&FQueue);
for(i=0;i<num;i++)
{
if(person[i].sex=='F')
enqueue(&FQueue,person[i]);
else
enqueue(&MQueue,person[i]);
}
printf("the dancers are:\n");
while(!is_empty(&FQueue)&&!is_empty(&MQueue))
{
p=dequeue(&FQueue);
printf(" %s ",p.name);
p=dequeue(&MQueue);
printf(" %s \n",p.name);
}
if(!is_empty(&FQueue))
{
printf(" there are %d women waiting for the nxt round!\n",FQueue.count);
p=dequeue(&FQueue);
printf("%s will be the first in the next round!\n",p.name);
}
if(!is_empty(&MQueue))
{
printf(" there are %d men waiting for the nxt round!\n",MQueue.count);
p=dequeue(&MQueue);
printf("%s will be the first in the next round!\n",p.name);
}
}
- 找舞伴
- 舞伴问题
- 舞伴问题
- 舞伴问题
- 舞伴问题
- 数据结构 舞伴问题
- ACM之舞伴问题
- 男女舞伴贪心
- 舞伴配对问题
- 舞伴问题(链队)
- 队列实现舞伴配对
- 舞伴配对问题
- 舞伴配对问题
- 舞伴配对问题
- 舞伴问题(2)
- C语言实现舞伴问题
- 简单的舞伴问题实现
- 【汕头市选2014】舞伴(perm)
- java中volatile的作用
- cocos2d-x v2.2 移植WP8详解
- 括号配对
- win32+vs2005以上+cuda开发环境搭建
- 长按(long Press)手势
- 找舞伴
- WebSocket 的拯救
- 2013-11-02 实验之泊松分酒(思维逻辑题)
- ACM->CF
- java 操作符
- 号码归属地查询
- 二分查找,二叉查找树(二叉排序树)的基本思想以及算法实现
- 高精度乘法
- llvm 学习笔记一 编译llvm