Day 21: Developing Programming Habits in 21 Days: C++ Problem Solving Day 21

Learn programming with Lao Ma by “leveling up and fighting monsters”!

  • Involves examination: Computer Society Programming Ability Level Certification (GESP)
  • Activity content: Provides real exam questions of different levels for students to choose for practice
  • Preparation advice: Choose corresponding questions based on your preparation level
  • Additional value: Can be used as preparation training for whitelist competitions

Day 21: GESP Level 1 2024.06_Cubic Numbers

[Submit]

https://www.luogu.com.cn/problem/B4001

[Problem Description]

Little Yang has a positive integer , and he wants to know if it is a cubic number.

A positive integer is a cubic number if and only if there exists a positive integer such that .

[Input Description]

The first line contains a positive integer .

[Output Description]

If the positive integer is a cubic number, output <span>Yes</span>, otherwise output <span>No</span>.

[Sample Input 1]

8

[Sample Output 1]

Yes

[Sample Input 2]

9

[Sample Output 2]

No

[Sample Explanation]

For Sample 1, there exists a positive integer such that , thus is a cubic number.

For Sample 2, there is no positive integer that satisfies the condition, thus is not a cubic number.

[Data Range]

For all data, it is guaranteed that .

[Reference Answer]

Method 1:

/*
* GESP2024.06 Cubic Numbers
* https://www.luogu.com.cn/problem/B4001
*/
#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int fl = 0;
    for (int i = 1;i <= n;i++)
    {
        if (i * i * i == n)
        {
            fl = 1;
            break;
        }
    }
    if (fl) cout << "Yes\n";
    else cout << "No\n";
}

Method 2:

/*
* GESP2024.06 Cubic Numbers
* https://www.luogu.com.cn/problem/B4001
*/
# include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for (int i = 1; i * i * i <= n; i++)
    {
        if (i * i * i == n)
        {
            cout << "Yes";
            return 0;
        }

    }
    cout << "No";
    return 0;
}

Day 21: GESP Level 2 2024.12_Find the Number

[Submit]

https://www.luogu.com.cn/problem/B4064

[Problem Description]

Little Yang has a positive integer , and he wants to know if there exists a positive integer such that .

[Input Description]

The first line contains a positive integer , representing the number of test cases.

For each test case, the first line contains a positive integer representing .

[Output Description]

For each test case, if there exists a positive integer that satisfies the condition, output , otherwise output .

[Sample Input 1]

3
16
81
10

[Sample Output 1]

2
3
-1

[Data Range]

For all data, it is guaranteed that ,.

Reference Answer:

/*
* [GESP202412 Level 2] Find the Number
* https://www.luogu.com.cn/problem/B4064
*/
#include <iostream>
#include <cmath>

using namespace std;

int main() 
{
    int t, a;
    cin >> t;
    for (int i = 0;i < t;i++)
    {
        cin >> a;
        int b = sqrt(sqrt(a));
        if (b * b * b * b == a)
        {
            cout << b << endl;
        }
        else
        {
            cout << -1 << endl;
        }
    }
    return 0;
}

Day 21: Openjudge 1.6.04_Array Reverse Replay

[Submit]

http://noi.openjudge.cn/ch0106/04/

[Description]

Rearrange the values in an array in reverse order. For example, the original order is . The requirement is to change it to .

[Input]

Input consists of two lines:

The first line is the number of elements in the array;

The second line contains integers, separated by spaces.

[Output]

Output a single line: the integers of the reversed array, separated by spaces.

[Sample Input]

5
8 6 5 4 1

[Sample Output]

1 4 5 6 8

[Reference Program]

C Language Version

/*
* 04: Array Reverse Replay
* http://noi.openjudge.cn/ch0106/04/
*/ 
#include <cstdio>

int main()
{
    int n, p1, p2;
    scanf("%d", &n);
    int a[100] = { 0 };
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    p1 = 0;
    p2 = n - 1;
    while (p1 < p2)
    {
        int temp = a[p1];
        a[p1] = a[p2];
        a[p2] = temp;
        p1++;
        p2--;
    }
    for (int i = 0; i < n; i++)
    {
        if (i == 0)
        {
            printf("%d", a[i]);
        }
        else
        {
            printf(" %d", a[i]);
        }
    }
    return 0;
}

C++ Version

/*
* 04: Array Reverse Replay
* http://noi.openjudge.cn/ch0106/04/
*/ 
# include <iostream>

using namespace std;

int main()
{
    int n,p1,p2;
    cin >> n;
    int a[100] = { 0 };
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    p1 = 0;
    p2 = n-1;
    while (p1 < p2)
    {
        int temp = a[p1];
        a[p1] = a[p2];
        a[p2] = temp;
        p1++;
        p2--;
    }
    for (int i = 0; i < n; i++)
    {
        if (i == 0)
        {
            cout << a[i];
        }
        else
        {
            cout << ' ' << a[i];
        }
    }
    return 0;
}

Day 21: GESP Level 4 2025.06_Canvas Cropping

[Submit]

https://www.luogu.com.cn/problem/B4360

[Problem Description]

Little A has drawn a picture on a rectangular canvas with height and width . Due to excessive white space at the edges, Little A wants to crop the canvas appropriately, keeping only the main part of the painting. Specifically, the canvas can be viewed as a character matrix with rows and columns, where the characters are all visible characters with ASCII codes between and . Little A only wants to keep the submatrix formed by rows from to and columns from to .

Little A hands the canvas to you; can you help him complete the cropping?

[Input Description]

The first line contains two positive integers , representing the number of rows and columns of the canvas.

The second line contains four positive integers, representing the boundaries of the rows and columns to keep.

Next, lines, each containing a string of length , represent the content of the canvas.

[Output Description]

Output a total of lines, each containing a string of length , representing the cropped canvas.

[Sample Input 1]

3 5
2 2 2 4
.....
.>_<.
.....

[Sample Output 1]

>_<

[Sample Input 2]

5 5
1 2 3 4
AbCdE
fGhIk
LmNoP
qRsTu
VwXyZ

[Sample Output 2]

Cd
hI

[Data Range]

For all test points, it is guaranteed that ,,.

Reference Answer:

/*
* [GESP202506 Level 4] Canvas Cropping
* https://www.luogu.com.cn/problem/B4360
*/
#include <iostream>

using namespace std;

char arr[105][105];

int main() 
{

    int h, w;
    cin >> h >> w;
    int x1, x2, y1, y2;
    cin >> x1 >> x2 >> y1 >> y2;
    for (int i = 1;i <= h;i++)
    {
        for (int j = 1;j <= w;j++)
        {
            cin >> arr[i][j];
        }
    }
    for (int x = x1;x <= x2;x++)
    {
        for (int y = y1;y <= y2;y++)
        {
            cout << arr[x][y];
        }
        cout << endl;
    }
    return 0;
}

Day 21: GESP Level 5 2025.03_Average Distribution

[Submit]

https://www.luogu.com.cn/problem/P11960

[Problem Description]

Little A has items, and Little B and Little C want to buy these items from Little A. For the th item, Little B will buy it at a price of , while Little C will buy it at a price of . To average the distribution of these items, Little A decides that Little B and Little C can each only buy exactly items. Can you help Little A find the maximum income he can earn from selling these items?

[Input Description]

The first line contains a positive integer .

The second line contains integers .

The third line contains integers .

[Output Description]

One line, an integer representing the answer.

[Sample Input 1]

3
1 3 5 6 8 10
2 4 6 7 9 11

[Sample Output 1]

36

[Sample Input 2]

2
6 7 9 9
1 2 10 12

[Sample Output 2]

35

[Data Range]

For test points, it is guaranteed that .

For the other test points, it is guaranteed that ,.

For all test points, it is guaranteed that ,.,.

Reference Answer:

/*
* [GESP202503 Level 5] Average Distribution
* https://www.luogu.com.cn/problem/P11960
*/
# include <iostream>
# include <vector>
# include <algorithm>

using namespace std;

struct node
{
    long long b, c, diff;
};

bool cmp(node x,node y)
{
    return x.diff < y.diff;
}

int main()
{
    vector<node> lst;
    int n;
    cin >> n;
    for (int i = 0;i < 2 * n;i++)
    {
        node a;
        cin >> a.b;
        lst.push_back(a);
    }
    for (int i = 0;i < 2 * n;i++)
    {
        cin >> lst[i].c;
        lst[i].diff = lst[i].b - lst[i].c;
    }
    sort(lst.begin(), lst.end(), cmp);
    long long ans = 0;
    for (int i = 0;i < n;i++)
    {
        ans += lst[i].c;
    }
    for (int i = n;i < 2 * n;i++)
    {
        ans += lst[i].b;
    }
    cout << ans;
    return 0;
}

Day 21: GESP Level 6 2024.09_Little Yang and Integer Splitting

[Submit]

https://www.luogu.com.cn/problem/P11246

[Problem Description]

Little Yang has a positive integer , and he wants to split it into several sums of perfect squares, while hoping to minimize the number of splits.

Write a program to calculate the minimum number of perfect squares that sum up to .

[Input Description]

The input consists of a single line with a positive integer .

[Output Description]

Output a single line with an integer representing the answer.

[Sample Input 1]

18

[Sample Output 1]

2

[Explanation/Hint]

For all test data, it is guaranteed that .

[Sample Output 2]

Reference Answer:

/*
* [GESP202409 Level 6] Little Yang and Integer Splitting
* https://www.luogu.com.cn/problem/P11246
*/
#include <iostream>
# include <climits>

using namespace std;

int n,len,dp[100005];
int a[400];// Record perfect squares

int dfs(int x, int pos)
{
    if (x == 0)
    {
        return 0;
    }
    if (dp[x])
    {
        return dp[x];
    }
    dp[x] = INT_MAX;
    for (int i = pos;i <= len && a[i] <= x;i++)
    {
        dp[x] = min(dp[x], dfs(x - a[i], pos) + 1);
    }
    return dp[x];
}

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

Youth Programming Competition Communication

The “Youth Programming Competition Communication Group” has been established (suitable for youth aged 6 to 18). Add the assistant’s WeChat to invite everyone into the learning group. After joining the group, everyone can participate in regularly organized 21-day problem-solving check-ins, level examination assessments, Ministry of Education whitelist competition coaching, and youth programming team competitions, etc.

Day 21: Developing Programming Habits in 21 Days: C++ Problem Solving Day 21

Leave a Comment