| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1787 人关注过本帖
标题:舞伴问题算法
只看楼主 加入收藏
Gates2
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-8-18
收藏
 问题点数:0 回复次数:2 
舞伴问题算法
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#define MAX_DANCERS 100
#define Queuesize 100//假定预分配的队列空间最多为100个元素
typedef struct{
char name[20];
char sex;//性别,‘F’代表女性,‘M’代表男性
}Person;
typedef Person DataType;
typedef struct{
    DataType data[Queuesize];
    int front;
    int rear;
    int count;
}CirQueue;

void Initial(CirQueue *Q)
{
    Q->front=Q->rear=0;
    Q->count=0;
}

int IsEmpty(CirQueue *Q)
{
   
    return Q->front==Q->rear;
}

int IsFull(CirQueue *Q)
{
    return Q->rear==Queuesize-1+Q->front;
}

void EnQueue(CirQueue *Q,DataType x)
{
    if(IsFull(Q))
    {
        printf("队列上溢");
        exit(1);
    }
    Q->count++;
    Q->data[Q->rear]=x;
    Q->rear=(Q->rear+1)%Queuesize;
}

DataType DeQueue(CirQueue *Q)
{
    DataType temp;
    if(IsEmpty(Q))
    {
        printf("队列为空");
        exit(1);
    }
    temp=Q->data[Q->front];
    Q->count--;
    Q->front=(Q->front+1)%Queuesize;
    return temp;
}

DataType Front(CirQueue *Q)
{
    if(IsEmpty(Q))
    {
        printf("队列为空");
        exit(1);
    }
    return Q->data[Q->front];
}

void DancePartner(Person dancer[],int num)
{
    int i;
    Person p;
    CirQueue Mdancers,Fdancers;
    Initial(&Mdancers);
    Initial(&Fdancers);
    for(i=0;i<num;i++)
    {
        p=dancer[i];
        if(p.sex=='F')
            EnQueue(&Fdancers,p);
        else if(p.sex=='M')
            EnQueue(&Mdancers,p);
    }
    printf("舞队是:\n\n");
    while(!IsEmpty(&Fdancers)&&!IsEmpty(&Mdancers)){
        p=DeQueue(&Fdancers);
        printf("%s          ",p.name);
        p=DeQueue(&Mdancers);
        printf("%s\n",p.name);
    }
    if(!IsEmpty(&Fdancers)){
        printf("\n还有 %d 个女士等下一轮.\n",Fdancers.count);
        p=Front(&Fdancers);
        printf("%s will be first to get a partner.\n",p.name);
    }
    else if(!IsEmpty(&Mdancers)){
        printf("\n 还有%d 个男士等下一轮.\n",Mdancers.count);
        p=Front(&Mdancers);
        printf("%s will be first to get a partner.\n",p.name);
    }
}

void InitialDancer(Person dancer[],int n)
{
    int i;
    printf("请输入参加跳舞的舞者:\n\n");//跳舞报名
        for(i=0;i<n;i++){
            dancer[i].sex=getchar();
            if(dancer[i].sex=='a') break;//结束标志
            gets(dancer[i].name);
            
    }
   
}

void main()
{
    Person dancer[MAX_DANCERS];
    int n=93;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
    InitialDancer(dancer,n);
    DancePartner(dancer,n);
}

第一次发帖多多顶顶谢谢!!!

搜索更多相关主题的帖子: 算法 舞伴 
2009-11-17 18:01
语言
Rank: 2
等 级:论坛游民
帖 子:17
专家分:19
注 册:2009-10-24
收藏
得分:0 
qian ge shafa  zuozuo
2009-11-18 22:43
pxhn111
Rank: 2
等 级:论坛游民
帖 子:15
专家分:10
注 册:2010-8-2
收藏
得分:0 
给个测试数据看看嘛
2010-08-04 21:02
快速回复:舞伴问题算法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012661 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved