21-22 solution to the 4th online competition

Although I ran away with him (educated)

But you can write it if you want  

Missing numbers

Meaning:

Judge whether there are 10 numbers 0 ~ 9 in each digit of a positive integer, and count how many numbers have not appeared.

Solution:

Use bool array to mark whether it has occurred

  • Initialization using memset
  • Use string type to read data, with s[i]-'0' as subscript

Upper board:

using namespace std;
bool num[10];
int main() {
    int T;cin >> T;
    while (T--) {
        int ans = 0;
        memset(num, 0, sizeof(num));//initialization num array
        string s;
        cin >> s;
        for (int i = 0;i < s.length();i++)
            num[s[i] - '0'] = 1;
        for (int i = 0;i < 10;i++)
            if (!num[i])ans++;
        cout << ans << endl;
    }
    return 0;
}

Favorite number

Meaning:

Specifies that tql likes the integer power of 2 and the integer power of 2 - 1

Given a number num, find the nearest conforming number from num

Solution:

Preprocess and record tql favorite numbers in all cases

Note: 2 ^ 17 = 131072, so the calculation amount of this problem is not large, and it should be able to solve it

  • Use the set container to store (you can replace it with an array. Remember to sort the array with sort)
  • Using lower_bound(begin,end,num) lookup (lower_bound is used for the sorted array)

Upper board:

using namespace std;
int main() {
    set<int>s;
    int a = 1;
    for (int i = 1;i < 18;i++) {
        a <<= 1;
        s.insert(a - 1);
        s.insert(a);
    }
    int t;cin >> t;
    int num;
    while (t--) {
        cin >> num;
        int num1 = *lower_bound(s.begin(), s.end(), num);//Binary search, return greater than or equal to num Minimum address of the value of
        int num2 = (num1 + 1 >> 1);//Pay attention to understand the meaning here num Why+1,You can try it on behalf of a group of data
        cout << (num1 - num < num - num2 ? num1 : num2) << endl;//Output values with smaller distances
    }
    return 0;
} 

OJ newcomer

Meaning:

How many points can the best question order have less than the current order?

Solution:

  1. Calculate the current total score sum1
  2. Calculate the time of each question and record it in the array a []
  3. Sort a
  4. Start with the topic with the shortest time

Fibonacci soup

Meaning:

A sequence satisfies: vi=vi-1/2+vi-2/3, I > = 3; In addition, every multiple to 5 will be + c. now give the first two terms and c to find the m-th term

Solution:

Just violence

Upper board:

using namespace std;
typedef long long ll;
int main() {
    int t;cin >> t;
    while (t--) {
        int a, b, c, m;
        cin >> a >> b >> c >> m;
        if (m == 1)cout << a << endl;
        else if (m == 2)cout << b << endl;
        else {
            for (int i = 1;i <= m;i++) {
                if (i == 1 || i == 2)continue;
                a = a / 3 + b / 2;
                if (i % 5 == 0)a += c;
                swap(a, b);
            }
            cout << b << endl;
        }
    }
    return 0;
}

Sum of common prime factors

Meaning:

Find the sum of all common prime factors of a and b.

Solution:

First find the greatest common factor of a and B, then find the prime factor of the greatest common factor, and then add and sum

 

The main problem is the method. In fact, the code implementation is not very difficult

Upper board:

using namespace std;
typedef long long ll;
int gcd(int a, int b)
{
    return !b ? a : gcd(b, a % b);
}
bool isPrime(int x) {
    if (x < 2) return false;
    for (int i = 2;i <= sqrt(x);i++)
        if (x % i == 0) return false;
    return true;
}
int main() {
    int t;cin >> t;
    while (t--) {
        int num1, num2;
        ll sum = 0;
        cin >> num1 >> num2;
        int num = gcd(num1, num2);
        for (int i = 2;i <= sqrt(num);i++) {
            if (num % i == 0 && isPrime(i))sum += i;
            if (num / i == i)continue;
            if (num % (num / i) == 0 && isPrime(num / i))sum += num / i;
        }
        if (isPrime(num))sum += num;
        cout << sum << endl;
    }
    return 0;
}

Maximum product

Meaning:

A positive even number m is divided into the sum of two prime numbers (which can be the same prime number), and the maximum product of the two prime numbers is output.

Solution:

  1. All prime numbers in the range are calculated using the sieve
  2. Start from m/2 (when two numbers are fixed, the closer the two numbers are, the greater the product of two numbers)

Upper board:

using namespace std;
typedef long long ll;
const int N = 1000007;
bool isprime[N];
int main() {
    memset(isprime, 1, sizeof(isprime));
    for (int i = 2; i * i < N; i++) {
        if (isprime[i]) {
            for (int j = i * i; j < N; j = j + i) {
                isprime[j] = 0;
            }
        }
    }
    int T;cin >> T;
    while (T--) {
        ll num;
        cin >> num;
        for (ll i = num / 2;i > 1;i--) {
            if (isprime[i] && isprime[num-i]) {
                cout << i * (num-i) << endl;
                break;
            }
        }
    }
    return 0;
}

 

 

Made by: BDT20040

PS: I was educated in this online game. When I saw the boss AK for 30 minutes, I blew my mind directly

Posted on Sat, 04 Dec 2021 13:47:18 -0500 by iFlex