China Electronics Society May 2023 Youth Software Programming C++ Level Examination Paper Level 4 Real Questions (Including Answers)

Insert image description here

1. Kaitou Kidd’s Glider

Kaito Kidd is a legendary thief, a super thief who specializes in jewelry. The most outstanding thing about him is that he can escape the heavy encirclement of the Nakamura Police Department every time, and this is largely due to the easy-to-operate hang glider he carries with him.
One day, Kaitou Kidd stole a precious diamond as usual, but his disguise was discovered by Conan's children, and the power device of his glider was also destroyed by the football kicked by Conan. As a last resort, Kaito Kidd can only use the damaged glider to escape.
Suppose there are N buildings in a line in the city, and each building has a different height. Initially, Kaito Kidd can be on top of any building. He can choose a direction to escape, but cannot change direction midway (because the Nakamori Police Department will pursue him). Because the glider's power unit was damaged, he could only glide downwards (that is, he could only glide from a higher building to a lower building). He wanted to pass over the tops of as many different buildings as possible, which would slow down the impact of the descent and reduce the chance of injury. Please tell me, how many tops of different buildings can he pass at most (including the initial building)?

【enter】

The first line of input data is an integer K (K < 100), which represents K sets of test data. Each set of test data contains two lines: the first line is an integer N (N < 100), representing N buildings. The second line contains N different integers, each corresponding to the height h of a building (0 < h < 10000), given in the order in which the buildings are arranged.

【Output】

For each set of test data, output a line containing an integer, representing the maximum number of buildings that Kaitou Kidd can pass.

[Sample input]

3
8
300 207 155 299 298 170 158 65
8
65 158 170 298 299 155 207 300
10
2 1 3 4 5 6 7 8 9 10

[Sample output]

6
6
9

Reference answer



2. Number combination

There are n positive integers, find the possible combinations in which the sum is t (t is also a positive integer). For example: n=5, the 5 numbers are 1,2,3,4,5 respectively, t=5; then there are three possible combinations: 5=1+4, 5=2+3 and 5=5.

【enter】

The first line of input is two positive integers n and t, separated by spaces, where 1<=n<=20 represents the number of positive integers, and t is the required sum (1<=t<=1000). Continue The next line contains n positive integers, separated by spaces.

【Output】

and is the number of different combinations of t.

[Sample input]

5 5
1 2 3 4 5

[Sample output]

3

Reference answer

#include <bits/stdc++.h>
using namespace std;

int a[25],n,t,ans;

void dfs(int i,int sum){
    
    
	if(i==n){
    
    
		if(t==sum){
    
    
			ans++;
		}
		return;
	}
	i++;
	dfs(i,sum+a[i]);
	dfs(i,sum);
}

int main(){
    
    
	cin>>n>>t;
	for(int i=1;i<=n;i++){
    
    
		cin>>a[i];
	}
	dfs(0,0);
	cout<<ans;
	return 0;
} 

3. String matching with wildcards

Wildcards are a type of keyboard characters that are often used to replace one or more real characters when we don't know the real characters or don't want to type the full name. Wildcard characters include question mark (?) and asterisk ( ), etc. Among them, "?" can replace one character, and " " can replace zero or more characters.
Your task is to determine whether they match, given a string with wildcard characters and a string without wildcard characters.
For example, 1?456 can match 12456, 13456, and 1a456, but cannot match 23456 and 1aa456; 2*77?8 can match 24457798, 237708, and 27798.

【enter】

The input has two lines, each line is a string of no more than 20 characters, the first line has wildcard characters, and the second line does not have wildcard characters.

【Output】

If the two can match, output "matched", otherwise output "not matched"

[Sample input]

1*456?
11111114567

[Sample output]

matched

Reference answer



4. Stock trading

Recently, more and more people are investing in the stock market, and Ah Fu is also a little excited. Keeping in mind that "there are risks in the stock market, you need to be cautious when entering the market", Afu decided to study a simplified version of stock trading issues first.
Assume that Afu has accurately predicted the price of a certain stock in the next N days, and he hopes to buy and sell it twice to maximize the profit. To keep the calculation simple, profit is calculated as the selling price minus the buying price.
Multiple sales and purchases can be made on the same day. But after the first purchase, you must first sell before you can buy the second time.
Now, Afu wants to know the maximum profit he can make.

【enter】

The first line of input is an integer T (T <= 50), which means there are T sets of data in total. For each subsequent set of data, the first line is an integer N (1 <= N <= 100, 000), indicating a total of N days. The second line is N integers separated by spaces, representing the price of the stock every day. The absolute value of the stock's daily price will not exceed 1,000,000.

【Output】

For each set of data, output one row. This row contains an integer representing the maximum profit that Afu can obtain.

[Sample input]

3
7
5 14 -2 4 9 3 17
6
6 8 7 4 1 -2
4
18 9 5 2

[Sample output]

28
2
0

提示:对于第一组样例,阿福可以第 1 次在第 1 天买入(价格为 5 ),然后在第 2 天卖出(价格为 14 )。第 2 次在第 3 天买入(价格为 -2 ),然后在第 7 天卖出(价格为 17 )。一共获得的利润是 (14 - 5) + (17 - (-2)) = 28 对于第二组样例,阿福可以第 1 次在第 1 天买入(价格为 6 ),然后在第 2 天卖出(价格为 8 )。第 2 次仍然在第 2 天买入,然后在第 2 天卖出。一共获得的利润是 8 - 6 = 2 对于第三组样例,由于价格一直在下跌阿福可以随便选择一天买入之后迅速卖出。获得的最大利润为 0

Reference answer


Guess you like

Origin blog.csdn.net/m0_46227121/article/details/131361066