当前位置:网站首页>7-18 simple simulation of banking business queue

7-18 simple simulation of banking business queue

2022-07-07 22:44:00 Qingshan's green shirt

Complete title

Set up a bank with A、B Two business windows , And the speed of processing business is different , among A The window processing speed is B Window 2 times —— When A Every time the window is finished 2 A customer ,B The window is finished 1 A customer . Given the sequence of customers arriving at the bank , Please output the customer sequence in the order of business completion . Let's say we don't consider the time interval of customers' arrival , And when different windows are processed at the same time 2 A customer ,A Window customer priority output .

Input format :

Enter a positive integer line , Among them the first 1 A digital N(≤1000) For the total number of customers , Follow behind N Number of customers . Customers with odd numbers need to go to A Window business , Even customers go to B window . Numbers are separated by spaces .

Output format :

Output customer number in the order of business processing . Numbers are separated by spaces , But there must be no extra space after the last number .

sample input :

8 2 1 3 9 4 11 13 15
No blank lines at the end

sample output :

1 3 2 9 11 4 13 15
No blank lines at the end

The easiest way to solve this problem should be to use three arrays , Because I am a beginner of data structure ( Also a computer beginner TAT), Take this to practice the queue , So the code is more complex . The queue in the question fully adapts to the first in, first out feature of the queue .

Specific ideas

1. Enter the number of operands n. Set an array to store this n Operands .
2. Set up two queues Q1,Q2,Q1 Deposit n Odd of the operands ,Q2 Store even numbers .
3. Output two odd numbers per , Just output an even law ( Key points !) Put this n Number from Q1 Q2 In and out of the team , Rearrange this n Put the number into the array ( Using the queue here is a little cumbersome , But it can be implemented with queues ), Finally, output one by one .
4. Note that there is no space at the end !

Complete code implementation :

1. Define the queue

#include<iostream>
using namespace std;
int a[1000];  // Global variables hhh

typedef struct {
         // Define a sequential stack 
    int data[1000];  // The queue length   notes : Title N<=1000 
    int front, rear; // Head pointer and tail pointer !
}SqQueue;

SqQueue CreateQ()     // Queue initialization   It can actually be in main Function   More concise .
{
    
    SqQueue Q;
    Q.rear = Q.front = 0;
    return Q;
}

int main()
{
    
    SqQueue Q1,Q2;            // Declare two queues 
    int n;				      // The number of operands 
    cin >> n;
    if (n > 1000 || n < 0)    //0 This critical value can be judged with or without 
        return 0;             //return Usage of ! Just jump out of the function !

    int str[n];            // Define an array to put here n Number ( So each number has a label )
    for (int i = 0; i < n; i++)
    {
    
        cin >> str[i];
    }
    
    // Here is a simpler initialization  OvO
   /* Q1.rear = Q1.front = 0; Q2.rear = Q2.front = 0;*/
    
    // Initialize two queues at the same time with a function 
    Q1 = CreateQ();
    Q2 = CreateQ();
    // hold n Put the number in two queues 
    for (int j = 0; j < n; j++)
    {
    
        if (str[j] % 2 == 0)// It's even 
        {
    // Insert queue 2
            Q2.data[Q2.rear] = str[j]; // Two cases , It's not good to put it in the same function  
            Q2.rear = Q2.rear + 1;//rear The pointer points to the position where the number is about to be stored !
        }
        else 
        {
    // Is odd   Insert queue 1
            Q1.data[Q1.rear] = str[j];
            Q1.rear = Q1.rear + 1;
        }
    }

        int s = 0;
        while(Q1.rear > Q1.front || Q2.rear > Q2.front)  // It has to be here ||!!! As long as there is one operation !
        {
    
            if (Q1.rear > Q1.front)// Is there a =?  No, ! Once again remind ! The tail pointer points to the position where the next element should be inserted !!! Not the last element !!
            {
      // Output two odd numbers 
                a[s] = Q1.data[Q1.front];
                s++;
                Q1.front = Q1.front + 1;
                a[s] = Q1.data[Q1.front];
                s++;
                Q1.front = Q1.front + 1;
            }
            if (Q2.rear > Q2.front)
            {
       // Output an even number 
                a[s] = Q2.data[Q2.front];
                s++;
                Q2.front = Q2.front + 1;
            }
        }
        
        for (int x = 0; x < n; x++)  // here s,n Fine !
        {
    
            if (x == n - 1)		  // Eliminate the last space !
            {
    
               cout << a[x];
                return 0;          // This sentence can't be lost ! Otherwise, two last numbers will be output 
            }
            cout << a[x] << " ";
        }
		// How to change every test point when the answer is wrong to see if the brackets are wrong woo woo 
		return 0;
}

Some of my questions

Here are some problems I encountered when doing this topic ( More basic QAQ)
1. How to store this n Operands are easy to read ?
Using arrays ! Array has subscript ! Also define the format of the array !

2. How to judge odd numbers and even numbers
Divide ( model ) Take the remainder of two to see if it is equal to zero TAT(a%2 == 0).

3. summary
(1)rear The pointer points to the position where the number is about to be stored !
(2) When eliminating the space after the last number, there must be one return Or in the cycle break, If you don't output the last two numbers .
(3) Single linked list is applicable Q1 = CreateQ( ); This kind of writing is feasible ! It's universal , Just queue is not necessary , It is easier to use it to create multiple linked lists in a single linked list !
(4) How to change every test point when the answer is wrong to see if the brackets are wrong woo woo …
(5)return Usage of ! Just jump out of the function !
(6) Pay attention to the queue length given in the title ! As in this question 1000
(7) The statement must be executed in the function body , Function can only be initialized outside
(8) Reviewed :
local variable (Local Variable): Variables defined inside the function body , The scope is limited to the function body . Leaving the function body will be invalid . Another call is an error .

Global variables (Global Variable): Definition : All functions are externally defined variables , Its scope is the whole program , That is, all the source files , Include .c and .h file .

End of the flower !
2021.10.2
21:23

原网站

版权声明
本文为[Qingshan's green shirt]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130603148125.html