C language, c
I. Task
One class has m girls and n boys (m is not equal to n). Now we have a dance. The boys and girls sat on the chairs on both sides of the dance floor. At the beginning of each song, one of the boys and the girls danced together, this song was not successful. The matching person sat and waited for the next song to seek a partner. After successfully pairing the dance, the matching partner goes back to the end of their respective teams.
Please design a system to simulate and dynamically display the above process, and require the output to calculate the dance number K for the first matching dance of any girl (number X) and any boy (number Y.
What data does X and Y in 100 dance songs fail to be paired with each other? Try it.
Ii. Program Analysis
1. Boys and girls are lined up in sequence. boys and girls in the right group are paired first, and the dance ends at the end of their respective teams. Therefore, this problem has a typical first-in-first-out feature. queues can be used as data storage methods.
2. The algorithm requires two queues: the boys' queue and the girls' queue. Each dance is an out queue and an in-queue for men and women.
3. During initialization, the values of boys' and girls' queues are their serial numbers.
4. The solution to this problem is similar to Experiment 5. Compare the matching numbers of girls and boys with the specified ones. If the numbers are equal, the current dance is the one to be calculated. Note that the number of dance songs must be within a certain range to avoid endless loops. For example, if no matching dance is performed between X and Y in the 100 dance songs, the matching is not successful.
Iii. debugging and Testing
1. Enter the number of girls and boys as prompted, and the number of girls and boys who wish to dance together. 6, 9, or 4, 7
2. Output: The X girls and Y boys are successfully paired with each other, and the number K of the dance music that X and Y are paired with for the first time is output. 16th songs
4. Programs 1 and 1 # include <stdio. h> 2 # include <stdlib. h> 3 // # include <process. h> 4 # define MAX 100 // max indicates the maximum queue length of 5 typedef int DataType; 6 typedef struct 7 8 {9 10 DataType elem [MAX]; /* define the array to store the data elements in the queue in sequence */11 int front;/* point to the subscript of the Header element */12 int rear; /* point to the next blank space of the team's tail element */13 14} Queue; 15 16 Queue InitQueue (); 17 // initialize and return an empty Queue 18 void ClearQueue (Queue Q ); 19 // clear the element 20 int IsEmpty (Queue Q) in Queue Q; 21 // determine whether the Queue is Null: Return Queue or non-empty flag 22 int Isfull (Queue Q); 23 // determine whether it is full, return the 24 Queue InserQ (Queue Q, dataType x); 25 // If the Queue is not full, add the data element x to the Queue 26 Queue DeleteQ (Queue Q); 27 // If the Queue is not empty, delete the first element 28 DataType GetHead (Queue Q); 29 // If the Queue is not empty, return the first element 30 DataType GetRear (Queue Q) in the Queue Q ); 31 // If the queue is not empty, the backend element 32 33 34 35 36 37 38 39 main () 40 {41 42 int m, n, song; 43 int I, j, k, x1, x2, v1, v2; 44 Queue Q1, Q2; 45 printf ("Input count of girls :" ); 46 scanf ("% d", & m); 47 printf ("Input count of boys:"); 48 scanf ("% d", & n ); 49 printf ("Input the songs number:"); 50 scanf ("% d", & song ); 51 52 if (m = n | n <0 | m <0) 53 {54 printf ("m = n | n <0 | m <0 input again! \ N "); 55 printf (" Input count of girls: "); 56 scanf (" % d ", & m); 57 printf (" Input count of boys: "); 58 scanf (" % d ", & n); 59} 60 61 Q1 = InitQueue (); 62 Q2 = InitQueue (); 63 64 for (I = 1; I <= m; I ++) 65 {66 Q1 = InserQ (Q1, I); 67 68} 69 70 for (j = 1; j <= n; j ++) 71 {72 Q2 = InserQ (Q2, j); 73 74} 75 76 v1 = v2 = 0; 77 for (k = 1; k <song; k ++) 78 {79 80x1 = GetHead (Q1); 81 v1 = x1; 82x2 = GetHead (Q2); 83 v2 = x2; 84 Q1 = DeleteQ (Q1); 85 Q2 = DeleteQ (Q2); 86 Q1 = InserQ (Q1, v1); 87 Q2 = InserQ (Q2, v2 ); 88 89} 90 91 printf ("Result: This is % d's girls and % d's boy dance! ", GetHead (Q1), GetHead (Q2); 92 93} 94 95 Queue InitQueue () 96 {97 Queue Q; 98 Q. front = Q. rear = 0; // queue initialization Q. front = Q. rear = 0 99 return (Q); 100} 101 102 Queue InserQ (Queue Q, DataType x) // If the Queue is not full, enter data element x into the team 103 {104 if (Q. rear + 1) % MAX = Q. front) 105 printf ("the queue is full and cannot enter"); 106 else107 {108 Q. elem [Q. rear] = x; // x into the queue 109 Q. rear = (Q. rear + 1) % MAX; // rear points to the next vacant position at the end of the Team 110} 111 return (Q); 112} 113 114 Queue DeleteQ (Queue Q) // If the queue is not empty, delete the first element 115 {116 if (Q. rear = Q. front) 117 printf ("the queue is empty and cannot leave the queue! "); 118 else119 Q. front = (Q. front + 1) % MAX; 120 return (Q); 121} 122 DataType GetHead (Queue Q) // If the Queue is not empty, assign a value to x124 {125 int x; 126 if (Q. rear = Q. front) 127 printf ("the queue is empty and cannot get the first line! "); 128 else129 x = Q. elem [Q. front]; 130 return (x); 131 132}View Code2, 1 # include <stdio. h> 2 # include <stdlib. h> 3 # define MAX 100 4 // max indicates the maximum queue length of 5 typedef int DataType; 6 typedef struct 7 {8 9 DataType elem [MAX]; /* define the array to store the data elements in the queue in sequence */10 int front;/* point to the subscript of the Header element */11 int rear; /* point to the next blank space of the team's tail element */12 13} Queue; 14 15 Queue InitQueue (); 16 // initialize and return an empty Queue 17 void ClearQueue (Queue Q ); 18 // clear element 19 int IsEmpty (Queue Q) in Queue Q; 20 // determine whether the Queue is empty, return Queue or non-empty mark 21 int I Sfull (Queue Q); 22 // determines whether the Queue is full. If the Queue is full or is not full, 23 Queue InserQ (Queue Q, DataType x) is returned. 24 // If the Queue is not full, add the data element x to the Queue 25 Queue DeleteQ (Queue Q); 26 // If the Queue is not empty, delete the first element 27 DataType GetHead (Queue Q ); 28 // If the Queue is not empty, the first element 29 DataType GetRear (Queue Q) in Queue Q is returned; 30 // If the Queue is not empty, return the team end element 31 32 main () 33 {34 35 int m, n, song; 36 int I, j, k, x, y, x1, x2, v1, v2; 37 Queue Q1, Q2; 38 printf ("Input count of girls:"); 39 scanf ("% d", & m); 40 printf ("I Nput count of boys: "); 41 scanf (" % d ", & n); 42 if (m = n | n <0 | m <0) 43 {44 printf ("m = n | n <0 | m <0 input again! \ N "); 45 printf (" Input count of girls: "); 46 scanf (" % d ", & m); 47 printf (" Input count of boys: "); 48 scanf (" % d ", & n); 49} 50 51 Q1 = InitQueue (); 52 Q2 = InitQueue (); 53 54 for (I = 1; I <= m; I ++) 55 {56 Q1 = InserQ (Q1, I); // assign a value of 57 58} 59 60 for (j = 1; j <= n; j ++) 61 {62 Q2 = InserQ (Q2, j ); /// assign a value to queue Q2 63 64} 65 66 printf ("Input number of girls:"); 67 scanf ("% d", & x ); 68 printf ("Input number of boys :" ); 69 scanf ("% d", & y); 70 71 if (x> m | y> n) 72 {73 printf ("x> m | y> n input again! \ N "); 74 printf (" Input number of girls: "); 75 scanf (" % d ", & x); 76 printf (" Input number of boys: "); 77 scanf (" % d ", & y); 78} 79 80 song = 1; 81 82 do 83 {84 v1 = v2 = 0; 85x1 = GetHead (Q1); // obtain the first line Q1 86 v1 = x1; 87x2 = GetHead (Q2); // obtain the first line Q2 88 v2 = x2; 89 Q1 = DeleteQ (Q1); // Delete the team header Q1 90 Q2 = DeleteQ (Q2); // Delete the team header Q2 91 Q1 = InserQ (Q1, v1 ); // return to team end Q1 92 Q2 = InserQ (Q2, v2); // return to team end Q2 93 song ++; 94} 95 while (GetHead (Q1 )! = X | GetHead (Q2 )! = Y); 96 printf ("Result: This is % d's girls and % d's boy dance! Song's number: % d ", x, y, song); 97 98} 99 100 Queue InitQueue () 101 {102 Queue Q; 103 Q. front = Q. rear = 0; // queue initialization Q. front = Q. rear = 0104 return (Q); 105} 106 107 Queue InserQ (Queue Q, DataType x) // if the Queue is not full, enter the data element x into the Queue 108 {109 if (Q. rear + 1) % MAX = Q. front) 110 printf ("the queue is full and cannot enter the queue"); 111 else112 {113 Q. elem [Q. rear] = x; // x into the queue 114 Q. rear = (Q. rear + 1) % MAX; // rear points to the next vacant position at the end of the team 115} 116 return (Q); 117} 118 119 Queue DeleteQ (Queue Q) // if the queue is not empty, delete the first element 120 {121 if (Q. rear = Q. front) 122 printf ("the queue is empty and cannot leave the queue! "); 123 else124 Q. front = (Q. front + 1) % MAX; 125 return (Q); 126} 127 DataType GetHead (Queue Q) // If the Queue is not empty, assign a value to x129 {130 int x; 131 if (Q. rear = Q. front) 132 printf ("the queue is empty and cannot get the first line! "); 133 else134 x = Q. elem [Q. front]; 135 return (x); 136 137}View Code