首页 > 其他分享 >舞伴配对模拟

舞伴配对模拟

时间:2023-04-12 21:02:43浏览次数:30  
标签:return int SqQueue 舞伴 rear front OK 配对 模拟

【问题描述】周末舞会上,男生和女生们分别进入舞厅,各自排成一队。假设每首舞曲只能有一名男生一名女生跳舞,跳舞开始,依次从男队和女队队头各取一人配成舞伴,若两队初始人数不同,则较长那一队未配对者等待下一轮舞曲。配对成功的舞伴跳完舞排回各自队伍的最后。现要求写一算法模拟上述舞伴配对问题。

【输入形式】第一行输入两个整数n和k,n(n<1000)表示入场人数,k表示舞曲数。接下来n行输入n个人的姓名和性别(姓名中间无空格),姓名和性别用空格分隔。

【输出形式】成功配对则依次输出每首舞曲配对跳舞的男生姓名和女生姓名,姓名之间以空格分隔,不能成功配对(男生队或女生队无人)则输出“error”。

【样例输入1】

5 5

Tom m

John m

Alice f

Jenny f

Mary f

【样例输出1】

Tom Alice

John Jenny

Tom Mary

John Alice

Tom Jenny

#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define MAXQSIZE 1000

typedef struct {
    char name[20];//姓名
    char sex;//性别
}Person;

typedef  Person ElemType;
typedef struct
{
    ElemType *base;
    int front;
    int rear;
}SqQueue;


int InitQueue(SqQueue *Q)
{
    Q->base=(ElemType *)malloc(MAXQSIZE*sizeof(ElemType));
    if(!Q->base)
        return ERROR;
    Q->front=Q->rear=0;
    return OK;
}/*InitQueue*/

/*队列长度*/
int QueueLength(SqQueue *Q)
{
    return (Q->rear-Q->front+MAXQSIZE)%MAXQSIZE;
}/*QueueLentgh*/

/*入队*/
int EnQueue(SqQueue *Q,ElemType e)
{
    if((Q->rear+1)%MAXQSIZE==Q->front)
        return ERROR;
    Q->base[Q->rear]=e;
    Q->rear=(Q->rear+1)%MAXQSIZE;
    return OK;
}/*EnQuese*/

/*出队*/
int DeQueue(SqQueue *Q,ElemType *e)
{
    if(Q->front==Q->rear)
        return ERROR;
    *e=Q->base[Q->front];
    Q->front=(Q->front+1)%MAXQSIZE;
    return OK;
}/*DeQueue*/

/*判队列是否为空*/
int QueueEmpty(SqQueue *Q)
{
    if(Q->front==Q->rear)
        return OK;
    else
        return ERROR;
}/*QueueEmpty*/

/*取对头*/
int GetHead(SqQueue *Q,ElemType *e)
{
    if(Q->front==Q->rear)
        return ERROR;
    *e=Q->base[Q->front];
    return OK;
}/*GetHead*/

/*释放队列*/
int DestroyQueue(SqQueue *Q)
{
    if(Q->base)
    {
        Q->rear=Q->front=0;
        free(Q->base);
    }
    return OK;
}/*DestroyQueue*/

int dancePartner(int n, int k)
{
    Person m, f, s;
    SqQueue Qm, Qf;
    if (!InitQueue(&Qm) || !InitQueue(&Qf))
        return ERROR;
    int i, j;
    for (i = 0, j = 0; i < n; i++)
    {
        scanf("%s %c", s.name, &s.sex);
        if (s.sex == 'm')
            EnQueue(&Qm, s);
        else if (s.sex == 'f')
            EnQueue(&Qf, s);
        j++;
    }
    if (j != n || QueueEmpty(&Qm) || QueueEmpty(&Qf))
    {
        printf("error");
        return ERROR;
    }
    while (k--)
    {
        DeQueue(&Qm, &m);
        DeQueue(&Qf, &f);
        printf("%s %s\n", m.name, f.name);
        EnQueue(&Qm, m);
        EnQueue(&Qf, f);
    }
    DestroyQueue(&Qm);
    DestroyQueue(&Qf);
    return OK;
}


int main()
{
    int n,k;
    scanf("%d %d",&n,&k);
    dancePartner(n,k);
    return 0;
}

标签:return,int,SqQueue,舞伴,rear,front,OK,配对,模拟
From: https://blog.51cto.com/u_16030624/6186228

相关文章

  • 4-20ma输入0-10v输出模拟量电流转换电压隔离模块
    主要特性:⑴精度等级:0.1级、0.2级、0.5级。产品出厂前已检验校正,用户可以直接使用⑵辅助电源:5V/12V/15V/24VDC或者220VAC(范围±10%)⑶国际标准二路信号输入:0-5V/0-10V/1-5V,0-10mA/0-20mA/4-20mA等⑷二路输出标准信号:0-5V/0-10V/1-5V,0-10mA/0-20mA/4-20mA等,具有高负载能力⑸全量......
  • BLE配对与绑定三
    前言:针对HID设备配对绑定获取信息常用代码做汇总。一、获取主机MAC地址需先绑定以获取固定MAC地址{gapBondRec_tbond_info;uint8_tadv_event_type=GAP_ADTYPE_ADV_HDC_DIRECT_IND;uint8_tAdv_Direct_Addr[B_ADDR_LEN];uint8_tAdv_Direct_Type=ad......
  • 直播平台软件开发,Android代码模拟触摸、点击及滑动等事件
    直播平台软件开发,Android代码模拟触摸、点击及滑动等事件一、应用中模拟物理和屏幕点击事件 例如,模拟对某个view的点击事件 privatevoidsimulateClick(Viewview,floatx,floaty){  longdownTime=SystemClock.uptimeMillis();  finalMotionEventdownEve......
  • UVa 253 Cube painting (模拟)
    253-CubepaintingTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=189Wehaveamachineforpaintingcubes.Itissuppliedwiththreedifferentcolors:bl......
  • UVa 706 / POJ 1102 LCD Display (模拟)
    706-LCDDisplayTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=647http://poj.org/problem?id=1102Afriendofyouhasjustboughtanewcomputer.Untilno......
  • UVa 489 Hangman Judge (模拟&字符串匹配)
    489-HangmanJudgeTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=430In``HangmanJudge,''youaretowriteaprogramthatjudgesaseriesofH......
  • UVa 11210 Chinese Mahjong (模拟&枚举&回溯)
    11210-ChineseMahjongTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2151Mahjong()isagameofChineseoriginusuallyplayedbyfourpersonswithtilesresemblingdominoesandbearing......
  • UVa 11507 Bender B. Rodríguez Problem (模拟&异或)
    11507-BenderB.RodríguezProblemTimelimit:4.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2502Benderisarobotbuiltby Mom'sFriendlyRobotCompany atits......
  • 4.8 模拟赛小记
    补。每次到模拟赛就真切的感觉到什么都不会了捏!感觉看着题目读了好几遍仍没有感觉,我不能感受到我的脑子在哪里。脑子在哪里呢?T1中位数考场想的暴力和正解有一丝的相似之处,但毕竟是暴力,90pts的暴力,更重要的是暴力写挂了。嘿嘿,统计了答案忘记往ans里去加了!非常的强大。关......
  • 模拟赛
    文件名请使用小写字母,记得加freopen。时间限制均为1s,空间限制均为512MiB。计算器(calc)题目描述请你编写一个最简单的计算器,支持+,-,*,/四种运算。保证输入输出均为整数,数据和运算结果不会超过int表示的范围。需要注意以下几种情况:若除数为\(0\)则输出Dividedbyz......