Data structure _ queue-loop Queue implementation simulation partner pairing problem

Source: Internet
Author: User
Tags rounds

Application of cyclic queue-partner pairing problem: At the party, the men and women are each lined up in a team. At the beginning of the dance, one of the men and the women's team was paired up with a partner. If the initial number of teams is not equal, the longer team is not paired to wait for the next round of dance. Assuming that the initial male, female and gender has been fixed, the number of rounds of the ball is entered from the keyboard. Try to solve the above partner pairing problem. Requirements: Output each round of partner pairing list from the screen, if there is an unpaired in the round, the name of the unpaired who is able to display the next round of the first appearance from the screen.

[CPP] View Plain copy print? #include  <stdio.h>   #include  <stdlib.h>   #include  <string.h >   typedef struct queue{       int Front;       int Rear;       char elem[100][100];        int Queuesize;  }queue;      void creat_ Queue (queue &q)    {//Set up a queue        int n,i;       Q.Front=Q.Rear=0;       printf ("Please enter the number of dancers:");   &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &n);       Q.Queuesize=n+1;       printf ("Please enter each dance name:");       for (i=0;i<n;i++)     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", &q.elem[i]);       q.rear=n;  }      int queueempty (queue q)     {//Determine if the queue is empty        if (q.front==q.rear)             return 1;       else            return 0;  }   Void dequeue (Queue &q,char &NBSP;*STR)    {//Remove team header elements        strcpy (Str,q.elem[q.front]);       q.front= (q.front+1)%q.queuesize;  }   void GetQueue (Queue &NBSP;Q,CHAR&NBSP;*STR)    {//Take the first element of the team, the team head pointer does not change        strcpy (str,q.elem[q. Front]);  }      void judge_queue (queue &m,queue &w)     {//partner pairing        int n;   &NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;STR1 [100],str2[100];       printf ("Please enter the number of rounds for the Prom:");       scanf ("%d" , &n);       while (n--)        {            while (! Queueempty (M))            {                if (Queueempty (W))                     dequeue (W,STR1);                dequeue (M,STR1);                dequeue (W,STR2);                printf ("Paired Dancer:%s %s\n", STR1,STR2);           }      &nbsP;    m.front= (m.front+1)%m.queuesize;            if (Queueempty (W))                     dequeue (W,STR1);            Getqueue (W,STR1);           printf ("name of the first appearance of the unpaired person:%s\n", str1) ;       }  }      int main ()    {        Queue M,W;       printf ("Male team: \ n");       creat_queue (M);       printf ("female team: \ n");        creat_queue (W);       if (m.queuesize>w.queuesize)             judge_queue (w,m);        else           judge_queue (m,w);           return 0;  }  

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct queue{int Front;
    int Rear;
    Char elem[100][100];
int queuesize;

}queue;
    void Creat_queue (Queue &q) {//build a queue int n,i;
    q.front=q.rear=0;
    printf ("Please enter the number of dancers:");
    scanf ("%d", &n);
    q.queuesize=n+1;
    printf ("Please enter each dance name:");
    for (i=0;i<n;i++) scanf ("%s", &q.elem[i]);
Q.rear=n;
    } int queueempty (queue Q) {//Determine if the queue is empty if (q.front==q.rear) return 1;
else return 0;
    } void DeQueue (Queue &q,char *str) {//delete team head element strcpy (Str,q.elem[q.front]);
Q.front= (q.front+1)%q.queuesize;

} void Getqueue (Queue Q,char *str) {//Take the first element of the team, the team head pointer does not change strcpy (Str,q.elem[q.front]);}
    void Judge_queue (Queue &m,queue &w) {//partner pairs int n;
    Char str1[100],str2[100];
    printf ("Please enter the number of rounds of the Dance:");
    scanf ("%d", &n); while (n--) {while (! Queueempty (M)) {if (Queueempty (W)) DeQueue (w,sTR1);
            DeQueue (M,STR1);
            DeQueue (W,STR2);
        printf ("Paired dancer:%s%s\n", STR1,STR2);
        } m.front= (m.front+1)%m.queuesize;
        if (Queueempty (W)) DeQueue (W,STR1);
        Getqueue (W,STR1);
    printf ("First appearance of the unpaired person's name:%s\n", str1);
    }} int main () {Queue m,w;
    printf ("Male team: \ n");
    Creat_queue (M);
    printf ("Women's team: \ n");
    Creat_queue (W);
    if (m.queuesize>w.queuesize) judge_queue (w,m);

    else Judge_queue (M,W);
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.