September 2022 C/C++ (Level 6) real test analysis#中国电子学院#National Youth Software Programming Level Examination

insert image description here

C/C++ Programming (Level 1~8) All real questions・Click here

Question 1: stack or queue

Both stacks and queues are commonly used linear structures, and they both provide two operations:
Push: Add an element.
Pop: Pops up an element.
The difference is that the stack is "first in, first out", while the queue is "first in, first out".
Given the order of entry and exit of a linear structure, determine whether the structure is a stack or a queue.
Time limit: 1000
Memory limit: 65535
input
Enter an integer t in the first line, which means there are t sets of test data For each set of test data, input an integer n in the first line, representing the number of operations. Then enter n lines, each containing two integers of type val. When type = 1, it indicates that the operation is a push operation, and val indicates the entered number. When type=2, it means that the operation is a pop operation, and the number represented by val. 3<=n<=2000
Output
Output one line for each set of test data. Output the linear structure corresponding to this set of data, "Stack" or "Queue". The title is guaranteed to be a type of stack or queue.
Sample input
2
6
1 1
1 2
1 3
2 3
2 2
2 1
4
1 1
1 2
2 1
2 2
Sample output
Stack
Queue

Here is an example solution implemented in C language:

#include <stdio.h>

#define MAX_SIZE 2000

typedef struct {
    
    
    int data[MAX_SIZE];
    int top;
} Stack;

typedef struct {
    
    
    int data[MAX_SIZE];
    int front;
    int rear;
} Queue;

void initStack(Stack *stack) {
    
    
    stack->top = -1;
}

void initQueue(Queue *queue) {
    
    
    queue->front = 0;
    queue->rear = -1;
}

void pushStack(Stack *stack, int value) {
    
    
    stack->data[++stack->top] = value;
}

int popStack(Stack *stack) {
    
    
    return stack->data[stack->top--];
}

void enqueue(Queue *queue, int value) {
    
    
    queue->data[++queue->rear] = value;
}

int dequeue(Queue *queue) {
    
    
    return queue->data[queue->front++];
}

int main() {
    
    
    int t;
    scanf("%d", &t);

    while (t--) {
    
    
        int n;
        scanf("%d", &n);

        int type, val;
        int isStack = 1; // Assume it's a stack initially
        int isQueue = 1; // Assume it's a queue initially

        Stack stack;
        Queue queue;

        initStack(&stack);
        initQueue(&queue);

        while (n--) {
    
    
            scanf("%d %d", &type, &val);

            if (type == 1) {
    
    
                pushStack(&stack, val);
                enqueue(&queue, val);
            } else if (type == 2) {
    
    
                if (isStack) {
    
    
                    if (val != popStack(&stack))
                        isStack = 0;
                }
                if (isQueue) {
    
    
                    if (val != dequeue(&queue))
                        isQueue = 0;
                }
            }
        }

        if (isStack && !isQueue)
            printf("Stack\n");
        else if (!isStack && isQueue)
            printf("Queue\n");
    }

    return 0;
}

This program first defines the structure of the stack (Stack) and queue (Queue), and then implements the function of initializing the stack and queue. Next, according to the input operation type and value, the operation of stack and queue is simulated by calling the corresponding function. In every stack or queue operation, it will be judged whether the characteristics of the stack or queue are met. If not, the corresponding flag bits (isStack and isQueue) will be set to 0. Finally, output the result according to the value of the flag.

Note: This program assumes that the input operation sequence is either a stack or a queue, and cannot satisfy the characteristics of both the stack and the queue.

Question 2: Basic operations of the stack

Stack is an important data structure, it has push k and pop operations. Push k is to add the number k to the stack, and pop is to take a number from the stack.
The stack is last-in-first-out: the stack is also regarded as a horizontal channel, then push k puts k on the far right of the stack, and pop also takes a number from the far right of the stack.
Assuming that the stack currently contains two numbers, 1 and 2, from left to right, the example diagram for performing push 5 and pop operations is as follows:
push 5 pop
1
stack 1 2 -------> 1 2 5 ------ > 1 2
Now, suppose the stack is empty. After a series of push k and pop operations, output the numbers stored in the stack. If the stack is already empty and the pop operation is still received,
an error will be output.
Time limit: 1000
Memory limit: 65536
input
The first line is m, which means there are m groups of test inputs, m<100. The first line of each group is n, which means there are n lines of push k or pop operations. (n<150) Next n lines, each line is push k or pop, where k is an integer. (Input ensures that the number in the stack will not exceed 100 at the same time)
Output
Output one line for each set of test data. Under normal circumstances, the content of this line is the number stored from left to right in the stack, and the numbers are directly separated by a space. If the stack is empty, no output will be made; but if the stack is empty and pop is received during the operation, it will be output error.
Sample input
2
4
push 1
push 3
pop
push 5
1
pop
Sample output
1 5
error

Here is an example solution implemented in C language:

#include <stdio.h>
#include <string.h>

#define MAX_SIZE 100

typedef struct {
    
    
    int data[MAX_SIZE];
    int top;
} Stack;

void initStack(Stack *stack) {
    
    
    stack->top = -1;
}

void push(Stack *stack, int value) {
    
    
    stack->data[++stack->top] = value;
}

int pop(Stack *stack) {
    
    
    return stack->data[stack->top--];
}

int isEmpty(Stack *stack) {
    
    
    return stack->top == -1;
}

int main() {
    
    
    int m;
    scanf("%d", &m);

    while (m--) {
    
    
        int n;
        scanf("%d", &n);

        Stack stack;
        initStack(&stack);

        int i;
        char operation[5];
        int value;

        for (i = 0; i < n; i++) {
    
    
            scanf("%s", operation);

            if (strcmp(operation, "push") == 0) {
    
    
                scanf("%d", &value);
                push(&stack, value);
            } else if (strcmp(operation, "pop") == 0) {
    
    
                if (isEmpty(&stack)) {
    
    
                    printf("error\n");
                    break;
                } else {
    
    
                    pop(&stack);
                }
            }
        }

        if (isEmpty(&stack))
            continue;

        while (!isEmpty(&stack)) {
    
    
            printf("%d ", pop(&stack));
        }

        printf("\n");
    }

    return 0;
}

The program first defines the structure of the stack (Stack), and then implements the functions of initializing the stack, pushing (push), popping (pop) and judging whether the stack is empty.

In the main function, first read the number m of test data, and then process each group of test data through a loop. For each set of test data, first read the number n of operations. Subsequently, the operation is read line by line through a loop, and the corresponding operation is performed according to the type of operation. If the stack is already empty when the pop operation is received, output "error" and jump out of the current loop. After n lines of operations are processed, if the stack is not empty, pop the stack one by one and output the numbers in the stack.

Note: According to the requirements of the title, if the stack is empty and the pop operation is still received, "error" will be output.

Question 3: Bad hair day

Some of Farmer John's N(1 ≤ N ≤ 80,000) cows may be having a bad hair day. Each cow is self-aware of its messy hairstyle, and Farmer John wants to know the sum of the number of cows that all cows can see on top of other cows.
The height of any cow i is recorded as hi (1 ≤ hi ≤ 1,000,000,000), and all cows face east (the right side of the schematic diagram of this question) and stand in a line in turn. Thus, cow i can see all cows in front of her (cows i+1, i+2...) that are shorter than her until she is blocked by a taller cow Consider the following example: = = =
-
=
Cows facing right ->
= = = =
- = = = =
= = = = = =
1 2 3 4 5 6 Cow #1 can see the head of cows #2, 3, 4
Cow #2 cannot see the head of any cow
# 3 can see overhead of cow #4
Cow #4 cannot see overhead of any cows
Cow #5 can see overhead of cow #6
Cow #6 cannot see overhead of any cows!
Let ci denote that cow i can see overhead cows number; please calculate the sum of c1 to cN. For the above example, the sum is: 3 + 0 + 1 + 0 + 1 + 0 = 5.
Time limit: 2000
Memory limit: 65536
input
Line 1: Number of cows N Line 2 to N+1 line: Line i+1 contains an integer indicating the height of cow i Output
Line
1: Cumulative sum of c1 to cN
Sample input
6
10
3
7
4
12
2
sample output
5

This problem can be solved by using a stack. We can traverse the cow's height from left to right and maintain an increasing stack, which stores the cow's height.

For each cow, we need to find the number of overhead cows it can see. We can start at the top of the stack and compare the height of the current cow with the height of the cow on top of the stack. If the height of the current cow is greater than the height of the top cow, then the top cow will not be seen by the current cow, and we can pop it from the stack. In the process, we can calculate the number of cows on top of the cow that the top cow can see and add them to the final result.

Finally, we push the height of the current cow into the stack. Repeat the above steps until all cows have been traversed.

Here is an example solution implemented in C language:

#include <stdio.h>

#define MAX_SIZE 80000

typedef struct {
    
    
    int height;
    int count;
} Cow;

typedef struct {
    
    
    Cow data[MAX_SIZE];
    int top;
} Stack;

void initStack(Stack *stack) {
    
    
    stack->top = -1;
}

void push(Stack *stack, Cow value) {
    
    
    stack->data[++stack->top] = value;
}

Cow pop(Stack *stack) {
    
    
    return stack->data[stack->top--];
}

Cow top(Stack *stack) {
    
    
    return stack->data[stack->top];
}

int main() {
    
    
    int N;
    scanf("%d", &N);

    Cow cows[MAX_SIZE];
    int i;

    for (i = 0; i < N; i++) {
    
    
        scanf("%d", &(cows[i].height));
        cows[i].count = 0;
    }

    Stack stack;
    initStack(&stack);

    long long total = 0;

    for (i = 0; i < N; i++) {
    
    
        while (stack.top >= 0 && cows[i].height >= top(&stack).height) {
    
    
            cows[i].count += top(&stack).count + 1;
            pop(&stack);
        }

        total += cows[i].count;
        push(&stack, cows[i]);
    }

    printf("%lld\n", total);

    return 0;
}

The program first defines the structure of the cow (Cow) and the stack (Stack). The cow structure contains the height of the cow and the number of cows that can be seen above the head, and the stack structure is used to store the cows.

In the main function, first read the number N of cows, and initialize the cow array. Then, use a loop to read the height of each cow from the input and store it in the cows array. At the same time, a stack is initialized.

Next, use a loop to iterate through the array of cows. For each cow, we start at the top of the stack and compare the height of the current cow with the height of the cow on top of the stack. If the height of the current cow is greater than the height of the top cow, we pop the top cow off the stack and count the number of cows on top that the top cow can see. Then, add this amount to the count attribute of the current cow. Repeat the above steps until the height of the current cow is less than or equal to the height of the cow on top of the stack. Finally, push the current cow into the stack.

After traversing all the cows, we add the count attribute of each cow to the sum total and output the result.

Note: Since the result may be very large, the long long type is used to store the sum total.

Question 4: Group Photo Effect

Xiaoyun and his friends went to climb Fragrant Hills. They were intoxicated by the beautiful scenery and wanted to take a group photo together. If they stand in a row, the boys are all on the left (from the perspective of the photographer) and line up from short to tall from left to right, and the girls are all on the right and line up from left to right in order of tall to short , May I ask what the effect of their group photo is like (all of them have different heights)?
Time limit: 1000
Memory limit: 65536
input
The first line is the number of people n (2 <= n <= 40, and there are at least 1 boy and 1 girl). Followed by n lines, each line enters a person's gender (male or female) and height (floating point number, in meters), and the two data are separated by spaces.
Output
n floating-point numbers to simulate the height of each person from left to right in the eyes of the photographer after standing in a team. Each floating-point number needs to be reserved to 2 decimal places, and two adjacent numbers are separated by a single space.
Sample input
6
male 1.72
male 1.78
female 1.61
male 1.65
female 1.70
female 1.56
Sample output
1.65 1.72 1.78 1.70 1.61 1.56

This problem can be solved by processing the input and arranging the heights of the people in a prescribed order.

First, we can create a struct to represent each person, including their gender and height information.

Then, we can store boys and girls in two different arrays according to the input gender and height information.

Next, we can sort the boys array from short to tall, and sort the girls array from tall to short.

Finally, we can create a result array and fill in the heights of boys and girls in the sorted order.

Here is an example solution implemented in C language:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 40

typedef struct {
    
    
    char gender[10];
    float height;
} Person;

int compare(const void *a, const void *b) {
    
    
    Person *personA = (Person *)a;
    Person *personB = (Person *)b;

    if (strcmp(personA->gender, "male") == 0 && strcmp(personB->gender, "male") == 0) {
    
    
        return personA->height - personB->height;
    } else if (strcmp(personA->gender, "female") == 0 && strcmp(personB->gender, "female") == 0) {
    
    
        return personB->height - personA->height;
    } else if (strcmp(personA->gender, "male") == 0 && strcmp(personB->gender, "female") == 0) {
    
    
        return -1;
    } else {
    
    
        return 1;
    }
}

int main() {
    
    
    int n;
    scanf("%d", &n);

    Person people[MAX_SIZE];
    Person males[MAX_SIZE];
    Person females[MAX_SIZE];

    int maleCount = 0;
    int femaleCount = 0;

    int i;
    for (i = 0; i < n; i++) {
    
    
        scanf("%s %f", people[i].gender, &(people[i].height));

        if (strcmp(people[i].gender, "male") == 0) {
    
    
            males[maleCount++] = people[i];
        } else {
    
    
            females[femaleCount++] = people[i];
        }
    }

    qsort(males, maleCount, sizeof(Person), compare);
    qsort(females, femaleCount, sizeof(Person), compare);

    float result[MAX_SIZE];
    int resultCount = 0;

    int maleIndex = 0;
    int femaleIndex = 0;

    for (i = 0; i < n; i++) {
    
    
        if (strcmp(males[maleIndex].gender, "male") == 0) {
    
    
            result[resultCount++] = males[maleIndex++].height;
        } else {
    
    
            result[resultCount++] = females[femaleIndex++].height;
        }
    }

    for (i = 0; i < n; i++) {
    
    
        printf("%.2f ", result[i]);
    }
    printf("\n");

    return 0;
}

The program first defines a structure representing a person (Person), which includes gender and height attributes.

In the main function, first read the number of people n, and create an array for storing all people (people) and arrays for boys (males) and girls (females).

Then, use a loop to read each person's gender and height from the input and store it in the corresponding array.

Next, use the qsort function to sort the array of boys and girls, and the sorting rules are defined by the compare function. The compare function first compares the gender. If they are both boys or girls, they are sorted according to height from short to tall or from tall to short; if one is a boy and the other is a girl, the boys are in front and the girls are in the back.

Finally, create a result array (result) and a count variable (resultCount), and use two index variables (maleIndex and femaleIndex) to point to the starting positions of the male and female arrays respectively. Use a loop to fill the heights of boys and girls into the result array in the sorted order, and increase the value of the count variable.

Finally, use a loop to output the heights in the resulting array with two decimal places separated by spaces.

Guess you like

Origin blog.csdn.net/gozhuyinglong/article/details/132638627