Learning today - Blue Bridge Cup 2019 C language group B

Posted by MortimerJazz on Mon, 03 Jan 2022 14:29:53 +0100

1. Hand Gan (personal experience, no big use, finally code)

 

 

2. Code (the following is what I can understand from the code answers of other bloggers. For details, please go to the following website directly)

https://blog.csdn.net/qq_44524918/article/details/113530469?spm=1001.2014.3001.5506

1) Question B: year string

[problem description]
Xiao Ming uses the letter A to correspond to the number 1, B to correspond to 2, and so on, and Z to correspond to 26. For numbers above 27, Xiaoming corresponds with a string of two or more bits, for example, AA corresponds to 27, AB corresponds to 28, AZ corresponds to 52, and LQ corresponds to 329.
What is the string corresponding to 2019?

[answer submission]
This is a question to fill in the blanks. You just need to calculate the results and submit them. The result of this question is a capital English string. When submitting the answer, only fill in this string. Pay attention to all capital letters. If you fill in excess content, you will not be able to score.

Problem solution
Answer: BYQ
This problem is actually a conversion from decimal to 26 base

#include <iostream>
using namespace std;

char str[27] = {0,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

int main(){
    int n;
    string ans;
    cin >> n;
    
    while(n){
        ans += str[n % 26];
        n /= 26;
    }
    
    int len = ans.length();
    for(int i=len-1; i>=0; i--)
        cout << ans[i];
    
    return 0;
}

 

Question C: sequence evaluation
[problem description]
Given the sequence 1, 1, 1, 3, 5, 9, 17,..., starting from item 4, each item is the sum of the first three items. Find the last four digits of item 20190324.

[answer submission]
This is a question to fill in the blanks. You just need to calculate the results and submit them. The result of this question is a 4-digit integer (hint: the thousand digit of the answer is not 0). When submitting the answer, only fill in this integer. If you fill in the redundant content, you will not be able to score.

Problem solution
Answer: 4659
This problem is similar to the Fibonacci sequence, but the final value may be very large, so we need to leave 10000 at each step, and each step will not change the final result, such as:

#include <iostream>
using namespace std;

const int mod = 1e4;
long long f[20190327]; 

int main(){
    f[1] = f[2] = f[3] = 1;
    
    for(int i=4; i<=20190324; i++){
        f[i] = (f[i-1] + f[i-2] + f[i-3]) % mod;
    }
    
    cout << f[20190324] << endl;;
    return 0;
}

 

Question D: decomposition of numbers
[problem description]
2019 is decomposed into the sum of three different positive integers, and each positive integer is required to contain no numbers 2 and 4. How many different decomposition methods are there? Note that the order of exchanging three integers is regarded as the same method, for example, 1000 + 1001 + 18 and 1001 + 1000 + 18 are regarded as the same method.

[answer submission]
This is a question to fill in the blanks. You just need to calculate the results and submit them. The result of this question is an integer. When submitting the answer, only fill in this integer. If you fill in the redundant content, you will not be able to score.

Problem solution
Answer: 40785

#include <iostream>
using namespace std;

bool check(int num)
{
    while (num)
    {
        if (num%10==2||num%10==4)
            return true;
        num/=10;
    }
    return false;
}
int main() {
    int res = 0;
    
    for (int i = 1; i < 2019 ; ++i) {
        if (check(i)) continue;
        for (int j = i+1; j < 2019 ; ++j) {
            if (check(j)) continue;
            for (int k = j+1; k < 2019 ; ++k) {
                if (check(k)) continue;
                if (i+j+k==2019)
                    res++;
            }
        }
    }
    cout << res << endl;
    return 0;
}

Question H: arithmetic sequence
Time limit: 1.0s memory limit: 256.0MB total score of this question: 20 points

[problem description]
The math teacher gave Xiao Ming a problem of summing the arithmetic sequence. But the careless Xiao Ming forgot part of the sequence and only remembered N integers.
Now give these N integers. Xiao Ming wants to know what is the shortest arithmetic sequence containing these N integers
How many?

[input format]
The first line of input contains an integer N.
The second line contains N integers A1, A2, ···, AN. (note that A1 ∼ AN is not necessarily given in the order of the arithmetic sequence)

[output format]
Output an integer representing the answer.

[sample input]
5
2 6 4 10 20
[sample output]

10
[example description]
The shortest arithmetic sequence containing 2, 6, 4, 10 and 20 is 2, 4, 6, 8, 10, 12, 14, 16, 18 and 20.

[evaluation case scale and agreement]
For all evaluation cases, 2 ≤ N ≤ 100000, 0 ≤ Ai ≤ 109.

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

const int max_n = 100005;
int a[max_n], N, res = 0;

int main(){
    cin >> N;
    set<int> d;
    
    for(int i=0; i<N; i++)
        cin >> a[i];
    
    sort(a, a+N); //Sort the known sequence from small to large
    
    for(int i=1; i<N; i++)//Find the minimum tolerance, set It can be sorted automatically, and the elements are unique 
        d.insert(a[i] - a[i-1]);
    int t = *(d.begin());//d The first element of is the minimum tolerance

    for(int i=a[0]; i<a[N-1]; i=i+t) res++; //Calculate the minimum term
    
    cout << res+1;
    return 0;
}

 

Topics: C++