2020 CSP-J Certification CCF Non-Professional Level Software Competency Certification First Round Real Questions--Reading Program Questions

2020 CCF certification first round (CSP-J) real questions

2. Reading program questions

(The input of the program does not exceed the range defined by the array or string, fill in the correct answer √ fill in the wrong answer X; unless otherwise specified, 1.5 points for the true answer, 3 points for the multiple choice question, a total of 4 points)

first question

01 #include <cstdlib>
02 #include <iostream>
03 using namespace std;
04
05 char encoder[26]= {'C','S','P',0};
06 char decoder[26];
07
08 string st;
09
10 int main(){
11    int k=0;
12    for (int i=0;i<26;++i)
13        if (encoder[i] != 0) ++k;
14    for (char x='A';x<='Z;++x){
15        bool flag = true;
16        for (int i=0;i<26; ++i)
17            if (encoder[i] == x){
18                flag = false;
19                break;
20            }
21        if (flag){
22            encoder[k] = x;
23            ++k;
24        }
25    }
26    for (int i=0;i<26;++i)
27        decoder[encoder[i]-'A']=i+'A';
28    cin >> st;
29    for (int i=0;i<st.length();++i)
30        st[i] = decoder[st[i] -'A'];
31    cout << st;
32    return 0;
33}

program analysis

It mainly examines children's ability to read and write programs and logical thinking ability. This program implements an encryption and decryption function; it uses a 26-element array encoder and a 26-element array decoder for encryption and decryption respectively.

The program first initializes the encoder array as {'C','S','P',0}, then finds the number of elements that are not zero by traversing the array, and outputs these elements. Next, the program iterates through all the letters from A to Z, checking to see if they are already present in the encoder array. If not already present, the letter is added to the encoder array. The program will also update the decoder array accordingly for decryption.

Then, the program gets a string from the user and decrypts each character in the string with the corresponding decoding method. Finally, the program outputs the decrypted string.

In short, this program implements a simple encryption and decryption function. The encryption method is to replace the three letters of capital C, S, and P with the three letters of capital A, B, and C. The specific letters before encryption and after encryption The letters are as follows:

Before encryption: ABCDEFGHIJKLMNOPQRSTUVWXYZ

After encryption: DEAFGHIJKLMNOPQCRBSTUVWXYZ

True or False

1), the input string should only consist of uppercase letters, otherwise it may be out of bounds when accessing the array

2), if the input string is not an empty string, the input string must be different from the output string

3) Change "i<26" in line 12 to "i<16", the program running result will not change

4), Change "i<26" in line 26 to "i<16", the program running result will not change

Answer: 1√ 2 × 3 √ 4 ×

Answer analysis:

1. From the program analysis, it can be concluded that the input can only be 26 uppercase letters

2. The input is the letter after S and the output is the same

3. The function of the 12th line is to read the number in the encoder array. Only three CSPs are given in the program, so changing it to 16 will not affect it, and changing it to 6 is fine, as long as it exceeds the number of encoder arrays.

4. The 26th is to decode 26 letters. If it is changed to 16, you will find that the latter part will be wrong

multiple choice

5), the output string is "ABCABCABCA", then the following statement is correct

A. There are both S and P in the input string

B. There are both S and B in the input string

C. There are both A and P in the input string

D. There are both A and B in the input string

Answer: A

Answer analysis: Because there are B and C in the output result, the input strings are S and P, so the answer A

6) If the output string is "CSPCSPCSPCSP", which of the following statements is correct

A. There are both P and K in the input string

B. There are both J and R in the input string

C. There are both J and K in the input characters

D. There are both P and R in the input string

Answer: D

Answer analysis: Because there are C and S in the output result, the input string is P and R, so the answer is D

second question

1 #include<iostream>
2 using namespace std;
3
4 long long n, ans;
5 int k, len;
6 long long d[1000000];
7
8 int main(){
9 	cin >> n >> k;
10	d[0] = 0;
11 	len= 1;
12	ans = 0;
13	for (long long i = 0; i<n; ++i) {
14		++d[0];
15		for (int j = 0;j + 1<len; ++j){
16			if (d[j] == k){
17				d[j] = 0;
18				d[j + 1] += 1;
19				++ans;
20			}
21		}
22		if (d[len- 1] == k){
23			d[len - 1] = 0;
24			d[len] =1;
25			++len;
26			++ans;
27		}
28	}
29	cout << ans << endl;
30	return 0;
31 }

program analysis

It mainly tests children's ability to read and write programs and logical thinking ability. It is relatively easy for children to make mistakes if they do not understand this program carefully, and they may even fail to understand the topic; the function to be realized by this program is to convert the decimal number n into the corresponding The k-ary number, the output result is the total number of carries during the conversion process, that is, ans; where len represents the length; when k is 1, n is 1, 1 in base 1, len is 2, so it is wrong; when input n is 1 and k > 1, ans == n; the maximum value of k-ary number converted from n to len digits is k^len - 1, so k^len>n.

True or False

1) If k=1, when outputting ans, len=n.

2) If k>1, when outputting ans, len must be smaller than n. 

3) If k>1, the output ans k^{only}must be greater than n. 

Answer: 1 × 2 × 3 √ 

multiple choice

4) If the input n is equal to 10^{15}and the input k is 1, then the output is equal to

A、1

B、\left ( 10^{30}-10^{15} \right )/2

C、\left ( 10^{30}+10^{15} \right )/2

D、10^{15}

Answer: D

5), if the input n is equal to 205,891,132,094,649 (that is 3^{30}), the input k is 3, then the output is equal to

A、3^{30}

B、\left ( 3^{30}-1 \right )/2

C、3^{30}-1

D、\left ( 3^{30}+1 \right )/2

Answer: B

6) If the input n is equal to 180,01,02,000,098 and the input k is 10, the output is equal to

A、11,112,222,444,543

B、11,122,222,444,453

C、11,122,222,444,543

D、11,112,222,444,453

Answer: B

Analysis of test points: 4. When the input k is 1, it means base 1, so it will enter every time, len is 2, but the condition of len++ cannot be triggered later, the result is that len ​​is always 2, every time d[ 0]++ will carry, output ans == n.

5. The 1st digit carries 1 carry for every k operations;
the 2nd digit carries 1 carry k^{2}for each operation;
...
so the 1st digit will generate 330/3 carries, and the 2nd digit will generate 330/ 3^{2}carries... ...the last digit, will generate 1 carry.
Therefore the answer = 3^{30}/ 3 + 3^{30}/ 3^{2}+ ... + 1
according to the summation formula Sn = ( 3^{30}– 1) / (3 - 1)

6. According to the above analysis, the answer can be obtained as B

third question

#include <algorithm>
#include <iostream>
using namespace std;

int n;
int d[50][2];
int ans;

void dfs(int n,int sum) {
	if (n == 1) {
		ans = max(sum, ans);
		return;
	}		
	for (int i=1;i<n;++i){
		int a= d[i -1][0],b = d[i - 1][1];
		int x=d[i][0],y = d[i][1];
		d[i-1][0]=a+x;
		d[i- 1][1]=b+y;
		for (int j=i;j<n-1;++j)
			d[j][0] = d[j + 1][0],d[j][1] = d[j + 1][1];
		int s=a+x+abs(b- y);
		dfs(n-1,sum +s);
		for (int j=n-1;j>i;--j)
			d[j][0] = d[j - 1][0],d[j][1]= d[j-1][1];
		d[i- 1][0]=a,d[i- 1][1]=b;
		d[i][0] =x,d[i][1] = y;
	}
}

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

program analysis

It mainly examines children's ability to read and write programs and logical thinking ability. The program implements depth-first search (DFS) recursively to find sub-arrays that meet the conditions. In the recursive process, maintain a sum of the currently selected elements, each time the current sub-array is expanded, add the element at the current position to the sum of the previously selected elements, and then add this sum to the current position and the next The absolute value of the difference at a position, as the new sum value. After the recursion is complete, update the answer ans to the current sum and the maximum value of ans.

Assuming that the input n is a positive integer not exceeding 5, and d[i][0]d[i][1] are all positive integers not exceeding 10000, complete the following judgment and multiple choice questions:

Judgment

1) If the input n is 0, the program may be in an infinite loop or an error may occur

2) If the input n is 20, and the next input is all 0, the output will be 0

3) The output number must not be less than any one of the input d[i][0] and d[i][1]

Answer: 1× 2√ 3 ×

Answer analysis: input 0, do nothing, and output 0 directly; input 20 0s, the sum is still 0, and ans is also 0; the input value may also be smaller than the input value, such as input n=2, 0 0 and 3 3. At this time, the output is 0

multiple choice

4) If the input n is 20, and the next input is 20 9s and 20 0s, the output is

A.1890        B.1881        C.1908        D.1917

Answer: B

5) If the input n is 3, and the next input is 3 0s and 3 5s, the output is

A.2000        B. 2010        C.2030        D.2020

Answer: C

6) If the input n is 15, the next input is 15 to 1, and 15 to 1, then the output is

A. 2440        B. 2220        C.2240        D. 2420

Answer: C

Test point analysis:

4. The second column is 0. Adding 0 equals no need to add. You can ignore
the 1st merge: 9+9=9* 2
the 2nd merge: 18+9=9*
3
the 3rd merge: 27+9 =9* 4
 …

19th time get: 9*20
so sum = 9* 2 + 9* 3 + ... + 9 * 20 = 1881

5. The first merge: 5-5=0
The second merge: 5+5-5=5=5* 1
The third merge: 10+5-5=10=5*
2
...
The 29th merge: 5* 30 - 5 = 5* 28
sum = 5 * (1 + 2 + ... + 28) = 2030

6. For column 1:
1st merge = 15+14
2nd merge = 15+14+13
...
14th merge = 15+14+13+12+...+1

15*14+14*14+13*13+…+1*1 = 1225

For column 2:
1st merge = 15-14
2nd merge = 15+14-13 3rd
merge = 15+14+13-12
...
14th merge = 15+14+13+12+ …+2-1
15*13+14*12+…+3*1=1001, add 14 1s at the end: 1015 The
final answer is: 2240

Guess you like

Origin blog.csdn.net/frank2102/article/details/131173715