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

Insert image description here

1. Programming questions (5 questions in total, 100 points in total)

1. Digital amplification

Given a sequence of integers and an amplification factor x, amplify each integer in the sequence x times and output it.
Time limit: 1000
Memory limit: 65536
[Input]
contains three lines: The first line is N, indicating the length of the integer sequence (N ≤ 100); the second line is N integers (not exceeding the integer range), and the integers are separated by one. Separated by spaces; the third line contains an integer (not exceeding the integer range), which is the specified integer x.

[Output]
N integers, which are the amplified sequences of the original sequence. The integers are separated by a space.

[Sample input]

3
1 5 7
2

[Sample output]

2 10 14

提示: 注意答案的数据范围

Reference answer

#include<bits/stdc++.h>
using namespace std;
long long a[105];
int main(){
    
    
  long long n , x;
  cin >> n;
  for(int i = 0;i < n;i++){
    
    
    cin >> a[i];
  }
  cin >> x;
  for(int i = 0;i < n;i++){
    
    
    cout << a[i] * x << " ";
  }
  return 0;
}

2. Word inversion

Write a program to read a line of English (containing only letters and spaces, with words separated by single spaces), reverse the order of all words and output them, still separated by single spaces.
Time limit: 10000
Memory limit: 65536
[Input]

Input is a string (string length is at most 100).
[Output]
The output is a string sorted as required.

[Sample input]

I am a student

[Sample output]

student a am I

Reference answer

#include<bits/stdc++.h>
using namespace std;
string s[100005];
int main(){
    
    
	int n = 0;
	while(cin >> s[n]){
    
    
		n++;
	}
	for(int i = n - 1;i >= 0;i--){
    
    
		cout << s[i] << " ";
	}
	return 0;
}

3. Calculate the sum of edge elements of the matrix

The so-called elements at the edge of the matrix are the elements in the first and last rows and the elements in the first and last columns.
Time limit: 10000
Memory limit: 65536
[Input]

The first line is the row number m and column number n of the matrix respectively (m < 100, n < 100), separated by a space. In the next m lines of data input, each line contains n integers, separated by a space.

【Output】

Output the sum of edge elements of the corresponding matrix

[Sample input]

3 3
3 4 1
3 7 1
2 0 1

[Sample output]

15

Reference answer

#include<bits/stdc++.h>
using namespace std;
int a[105][105];
int main(){
    
    
	int n , m , sum = 0;
	cin >> n >> m;
	for(int i = 0;i < n;i++){
    
    
		for(int j=0;j<m;j++){
    
    
			cin >> a[i][j];
		}
	}
	for(int i = 0;i < n;i++){
    
    
		for(int j = 0;j < m;j++){
    
    
			if(i == 0 || i == n - 1 || j == 0 || j == m - 1){
    
    
				sum = sum + a[i][j];
			}
		}
	}
	cout << sum;
	return 0;
}

4. Odd single increasing sequence

Given a sequence of positive integers of length N (not greater than 500), please take out all the odd numbers and output them in ascending order.
Time limit: 1000
Memory limit: 65536
[Input]

There are 2 lines in total: The first line is N; the second line is N positive integers, separated by spaces.

【Output】

A sequence of odd numbers output in increasing order, separated by commas. The data is guaranteed to have at least one odd number.

[Sample input]

10
1 3 2 6 5 4 9 8 7 10

[Sample output]

1,3,5,7,9

Reference answer

#include<bits/stdc++.h>
using namespace std;
int a[505];
int main(){
    
    
	int n;
	int ans = 0;
	cin >> n;
	for(int i = 0;i < n;i++){
    
    
		cin >> a[i];
	}
	sort(a + 0 , a + n);
	for(int i = 0;i < n;i++){
    
    
		if(a[i] % 2 != 0){
    
    
			if(ans == 1){
    
    
				cout<<",";
			}
			cout << a[i];
			ans = 1;
		}
	}
	return 0;
}

5. Addition of real numbers

Find the sum of two real numbers added together.

The floating point numbers appearing in the input and output in the question have the following forms: P1P2…Pi.Q1Q2…Qj. For the integer part, P1P2...Pi is a non-negative integer and when the integer part is not 0, P1 is not equal to 0; for the decimal part, Qj is not equal to 0.
Time limit: 1000
Memory limit: 65536
[Input]

2 lines, each line is an addend. The length of each addend cannot exceed 100.

【Output】

One row, the corresponding sum. The output is guaranteed to be a real number with a decimal part that is not 0.

[Sample input]

0.111111111111111111111111111111
0.111111111111111111111111111111

[Sample output]

0.222222222222222222222222222222

Reference answer

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
    
    
    char a[202],b[202],c[101],d[101];
    cin>>a;
    cin>>b;
    memset(c,0,sizeof(c));
    memset(d,0,sizeof(d));
    int i,j=0,ap=0,bp=0,ka=0,kb=0;
    //ka,kb用来统计数组a和b的小数部分的位数
    //ap,bp用来统计数组a和b的整数部分的位数
 
    //统计数组a的整数部分和小数部分的位数
    for(i=0; i<strlen(a); i++)
    {
    
    
        if(a[i]=='.')
        {
    
    
            break;
        }
        ap++;
    }
    ka=strlen(a)-ap-1;
    //统计数组b的整数部分和小数部分的位数
    for(i=0; i<strlen(b); i++)
    {
    
    
        if(b[i]=='.')
        {
    
    
            break;
        }
        bp++;
    }
    kb=strlen(b)-bp-1;
    //对于整数部分从个位开始加即倒叙输入
    for(i=0; i<ap||i<bp; i++)
    {
    
    
        if(i<ap)
        {
    
    
            c[i]+=a[ap-1-i]-'0';
        }
        if(i<bp)
        {
    
    
            c[i]+=b[bp-1-i]-'0';
        }
        if(c[i]>9) //大于9则向前进1
        {
    
    
            c[i+1]+=1;
            c[i]%=10;
        }
    }
    //判断是否有小数部分
    if(ka>0||kb>0)
    {
    
    
        for(i=0; i<ka||i<kb; i++)
        {
    
    
            if(i<ka)
            {
    
    
                d[i]+=a[i+ap+1]-'0';//小数部分正序输入
            }
            if(i<kb)
            {
    
    
                d[i]+=b[i+bp+1]-'0';
            }
        }
        //取两者最大小数位,用于输出小数输出
        if(ka<kb)
        {
    
    
            ka=kb;
        }
        //取两者最大整数位
        if(ap<bp)
        {
    
    
            ap=bp;
        }
        for(i=ka-1; i>0; i--) //向前进1
        {
    
    
            if(d[i]>9)//小数部分的进位
            {
    
    
                d[i-1]+=1;
                d[i]%=10;
            }
        }
        if(d[0]>9)//对第一位小数是否进位进行判断
        {
    
    
            c[0]+=1;
            d[0]%=10;
            while(c[j]>9)
            {
    
    
                c[j+1]+=1;
                c[j]%=10;
            }
        }
        if(c[ap]>0)
        {
    
    
            printf("%d",c[ap]);
        }
        for(i=ap-1; i>=0; i--)
        {
    
    
            printf("%d",c[i]);
        }
        printf(".");
        for(i=0; i<ka; i++)
        {
    
    
            printf("%d",d[i]);
        }
    }
    else
    {
    
    
        if(ap<bp)
        {
    
    
            ap=bp;
        }
        if(c[ap]>0)
        {
    
    
            printf("%d",c[ap]);
        }
        for(i=ap-1; i>=0; i--)
        {
    
    
            printf("%d",c[i]);
        }
    }
    return 0;
}

Guess you like

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