Partner problem PTA
describe
Assuming that the records of men and women are stored in an array, an algorithm is designed to realize partner pairing. It is required to output the paired partner and the name of the team head element without pairing.
Sample
sample input
Li minhao M
Li zhongshuo M
Gao Xinya F
Wu Yanzu M
Wang Sicong M
Zhang Tianyuan F
Zhang Zhilin M
Xu Dandan F
Ma Xiaoyun F
sample output
Gao Xinya, Li minhao
Zhang Tianyuan, Li zhongshuo
Xu Dandan, Wu Yanzu
Ma Xiaoyun, Wang Sicong
Zhang Zhilin
Function definition interface:
void DancePartner(DataType dancer[], int num) ;
Where dancer [] is the array for storing men's and women's information, and num is the size of the array.
Example of referee test procedure:
#include<stdio.h> #include<stdlib.h> typedef struct { char name[20]; char sex; } DataType; struct Node { DataType data; struct Node* next; }; typedef struct Node *PNode; struct Queue { PNode f; PNode r; }; typedef struct Queue *LinkQueue; LinkQueue SetNullQueue_Link() { LinkQueue lqueue; lqueue = (LinkQueue)malloc(sizeof(struct Queue)); if (lqueue != NULL) { lqueue->f = NULL; lqueue->r = NULL; } else printf("Alloc failure! \n"); return lqueue; } int IsNullQueue_link(LinkQueue lqueue) { return (lqueue->f == NULL); } void EnQueue_link(LinkQueue lqueue, DataType x) { PNode p; p = (PNode)malloc(sizeof(struct Node)); if (p == NULL) printf("Alloc failure!"); else { p->data = x; p->next = NULL; if (lqueue->f == NULL) { lqueue->f = p; lqueue->r = p; } else { lqueue->r->next = p; lqueue->r = p; } } } void DeQueue_link(LinkQueue lqueue) { struct Node * p; if (lqueue->f == NULL) printf("It is empty queue!\n "); else { p = lqueue->f; lqueue->f = lqueue->f->next; free(p); } } DataType FrontQueue_link(LinkQueue lqueue) { if (lqueue->f == NULL) { printf("It is empty queue!\n"); } else return (lqueue->f->data); } void DancePartner(DataType dancer[], int num) { /* Please fill in the answer here */ } int main() { DataType dancer[9]; for (int i = 0; i < 9; i++) scanf("%s %c", dancer[i].name, &dancer[i].sex); DancePartner(dancer, 9); return 0; }
Train of thought I
Only one queue is defined. If the paired queue is out of the queue, and the queue entry operation is not performed. Finally, print the names of unpaired team head elements.
void DancePartner(DataType dancer[], int num) { LinkQueue Queue_head = SetNullQueue_Link(); for (int i = 0; i < num; i++) { if (!IsNullQueue_link(Queue_head) && (FrontQueue_link(Queue_head).sex) != dancer[i].sex) { if (dancer[i].sex == 'F') printf("%s %s\n", dancer[i].name, Queue_head->f->data.name); else { printf("%s %s\n", Queue_head->f->data.name, dancer[i].name); } DeQueue_link(Queue_head); } else { EnQueue_link(Queue_head, dancer[i]); } } printf("\n"); if (!IsNullQueue_link(Queue_head)) { printf("%s", Queue_head->f->data.name); } }
Train of thought II
Define two queues, first enter the queue, then match to get out of the queue, and finally print the name of the unpaired queue header element
void DancePartner(DataType dancer[], int num) { LinkQueue Mdancers = SetNullQueue_Link(); LinkQueue Fdancers = SetNullQueue_Link(); for (int i = 0; i < num; ++i) { if (dancer[i].sex == 'M') EnQueue_link(Mdancers,dancer[i]); else{ EnQueue_link(Fdancers,dancer[i]); } if (dancer[i].sex == 'M'){ if (IsNullQueue_link(Fdancers) != 1){ printf("%s %s\n",FrontQueue_link(Fdancers).name,FrontQueue_link(Mdancers).name); DeQueue_link(Mdancers); DeQueue_link(Fdancers); } } else{ if(IsNullQueue_link(Mdancers) != 1){ printf("%s %s\n",FrontQueue_link(Fdancers).name,FrontQueue_link(Mdancers).name); DeQueue_link(Mdancers); DeQueue_link(Fdancers); } } } printf("\n"); if (IsNullQueue_link(Mdancers) != 1) printf("%s",FrontQueue_link(Mdancers).name); if (IsNullQueue_link(Fdancers) != 1) printf("%s",FrontQueue_link(Fdancers).name); }
END
I believe when you first looked at the queue, you were as boring as I was yesterday.
lqueue->f = NULL; lqueue->r = NULL;
Lqueue - > F in the queue points to the head of the queue (the place out of the queue), and lqueue - > R points to the tail of the queue (the place in the queue).
With the continuous reading and understanding of the above topics, I found that the queue has a more complex structure. The previous single linked list and double linked list were only used to serve the following queues, stacks and trees. If you carefully read the code of the topic and read the following sentence, you should understand this kind of doll.
Q
u
e
u
e
_
h
e
a
d
⟶
f
⟶
d
a
t
a
.
n
a
m
e
Queue\_head\longrightarrow{}f\longrightarrow{}data.name
Queue_head⟶f⟶data.name
Simplify the above formula
a
=
Q
u
e
u
e
_
h
e
a
d
⟶
f
a
⟶
d
a
t
a
.
n
a
m
e
a=Queue\_head\longrightarrow{}f\\ a\longrightarrow{}data.name
a=Queue_head⟶fa⟶data.name
a
⟶
d
a
t
a
.
n
a
m
e
a\longrightarrow{}data.name
Does a ⟶ data.name look familiar
struct Node { DataType data; struct Node* next; };
Look, there's next. This is a linked list.
For. data.name, some people also wonder how it came from. This is the usage of the structure. The usual arrow is to access the variables in the structure pointer. Forget your little partner and Baidu related knowledge points.
data is defined by DataType, and DataType is typedef in the following way
typedef struct { char name[20]; char sex; } DataType;
In the past, I only knew that typedef was useful, but I didn't quite understand it. After experiencing this problem, I understood it. Create a real cow batch of numerical type by yourself. You can also modify it with one click in large projects. It feels like a macro definition.
Well, the structure array is also used here.
DataType dancer[9];
I won't write it in detail. It ends abruptly.