Learn programming with Lao Ma by “leveling up and fighting monsters”!
- Involves examination: Computer Society Programming Ability Level Certification (GESP)
- Activity content: Provides different levels of real questions 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 15: GESP Level 1 2024.03_Xiao Yang Buys Books
[Submit]
https://www.luogu.com.cn/problem/B3952
[Problem Description]
Xiao Yang has saved some pocket money to buy books. Given that the price of a book is 13 yuan, write a program to calculate how many books he can buy with his pocket money and how much money he will have left.
[Input Description]
Input a positive integer, representing the amount of pocket money Xiao Yang has.
[Output Description]
Output two lines: the first line is the number of books purchased, and the second line is the remaining pocket money.
[Sample Input 1]
100
[Sample Output 1]
7
9
[Sample Input 2]
199
[Sample Output 2]
15
4
For all data, it is guaranteed that .
Reference Answer:
/*
* [GESP202403 Level 1] Xiao Yang Buys Books
* https://www.luogu.com.cn/problem/B3952
*/
# include
using namespace std;
int main()
{
int m;
cin >> m;
cout << m / 13 << endl;
cout << m % 13 << endl;
return 0;
}
Day 15: GESP Level 2 2025.03_Time Transition
[Submit]
https://www.luogu.com.cn/problem/B4260
[Problem Description]
Assuming the current date is year month day hours later is year month day hours, help Xiao Yang calculate the corresponding for the given .
[Input Description]
Input consists of five lines, each line containing a positive integer, representing .
[Output Description]
Output four positive integers, representing .
[Sample Input 1]
2008
2
28
23
1
[Sample Output 1]
2008 2 29 0
[Data Range]
For all data, it is guaranteed that ,,,,. Data is guaranteed to be valid time.
[Hint]
Leap year judgment rules
- Ordinary leap year: A year divisible by 4 but not by 100.
- Century leap year: A year divisible by 400.
A year that satisfies any of the above rules is a leap year; otherwise, it is a common year.
Reference Answer:
Method 1:
/*
* [GESP202503 Level 2] Time Transition
* https://www.luogu.com.cn/problem/B4260
*/
#include
using namespace std;
int main()
{
int y, m, d, h, k;
cin >> y >> m >> d >> h >> k;
h = h + k;
if (h >= 24)
{
d += 1;
h = h - 24;
}
if (m == 2)
{
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
{
if (d > 29)
{
m += 1;
d = d - 29;
}
}
else
{
if (d > 28)
{
m += 1;
d = d - 28;
}
}
}
else
{
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
if (d > 31)
{
m += 1;
d = d - 31;
}
}
else
{
if (d > 30)
{
m += 1;
d = d - 30;
}
}
}
if (m > 12)
{
y += 1;
m = m - 12;
}
cout << y << " " << m << " " << d << " " << h << endl;
return 0;
}
Method 2:
/*
* [GESP202503 Level 2] Time Transition
* https://www.luogu.com.cn/problem/B4260
*/
#include
#include
using namespace std;
int main()
{
int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int y, m, d, h, k;
cin >> y >> m >> d >> h >> k;
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
{
days[2] = 29;
}
h += k;
if (h > 23)
{
h = h - 24;
d += 1;
}
if (d > days[m])
{
d = d - days[m];
m += 1;
}
if (m > 12)
{
m = m - 12;
y += 1;
}
cout << y << " " << m << " " << d << " " << h;
return 0;
}
Day 15: Openjudge 1.6.01_Count of Numbers Equal to a Given Number
[Submit]
http://noi.openjudge.cn/ch0106/01/
[Description]
Output the count of numbers in an integer sequence that are equal to a specified number.
[Input]
Input consists of three lines:
The first line is , representing the length of the integer sequence;
The second line contains integers, separated by a space;
The third line contains an integer, which is the specified integer.
[Output]
Output the count of numbers that are equal to the specified number.
[Sample Input]
3
2 3 2
2
[Sample Output]
2
[Reference Program]
C Language Version
/*
* 01: Count of Numbers Equal to a Given Number
* http://noi.openjudge.cn/ch0106/01/
*/
#include
int main()
{
int nums[100] = { 0 };
int n, m, cnt = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &nums[i]);
}
scanf("%d", &m);
for (int i = 0; i < n; i++)
{
if (nums[i] == m)
{
cnt++;
}
}
printf("%d", cnt);
return 0;
}
C++ Version
/*
* 01: Count of Numbers Equal to a Given Number
* http://noi.openjudge.cn/ch0106/01/
*/
# include
using namespace std;
int main()
{
int nums[100] = { 0 };
int n, m, cnt = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> nums[i];
}
cin >> m;
for (int i = 0; i < n; i++)
{
if (nums[i] == m)
{
cnt++;
}
}
cout << cnt;
return 0;
}
Day 15: GESP Level 4 2023.09_Base Conversion
[Submit]
https://www.luogu.com.cn/problem/B3869
[Problem Description]
Base numbers refer to counting systems that carry over at a certain base. For example, people generally use decimal counting in daily life, while computers generally use binary at a lower level. In addition, octal and hexadecimal are also commonly used counting systems (in hexadecimal, letters A to F represent ten to fifteen; in this problem, bases eleven to fifteen are similar).
In this problem, we will provide different base numbers. You need to convert them to decimal numbers.
[Input Description]
The first line of input is a decimal integer. The next lines, each containing an integer followed by a space and then a base number, represent the number to be converted. It is guaranteed that all base numbers consist of digits and uppercase letters and do not start with 0. It is guaranteed that ; all base numbers have no more than 9 digits.
[Output Description]
Output lines, each representing the decimal value of the corresponding base number.
[Special Reminder]
In regular programs, providing prompts during input and output is a good habit. However, in this exam, due to system limitations, please do not include any prompt information in the input and output.
[Sample Input 1]
2
8 1362
16 3F0
[Sample Output 1]
754
1008
[Sample Input 2]
2
2 11011
10 123456789
[Sample Output 2]
27
123456789
Reference Answer:
/*
* GESP202309 Level 4 Base Conversion
* https://www.luogu.com.cn/problem/B3869
*/
#include
#include
using namespace std;
int func(char c)
{
if (c >= '0' && c <= '9')
{
return c - '0';
}
else
{
return c - 'A' + 10;
}
}
int main()
{
int N;
cin >> N;
for (int i = 1;i <= N;i++)
{
int K;
string s;
cin >> K >> s;
int len = s.size();
long long total = 0, p = 1;
for (int j = len - 1;j >= 0;j--)
{
total += func(s[j]) * p;
p *= K;
}
cout << total << endl;
}
return 0;
}
Day 15: GESP Level 5 2023.09_Skillfully Winning Prizes
[Submit]
https://www.luogu.com.cn/problem/B3872
[Problem Description]
Xiao Ming participated in a game show to win prizes. The host announced the game rules:
1. The game is divided into time slots, and participants can choose a mini-game in each time slot.
2. There are mini-games to choose from.
3. For the mini-game, participants must complete it before the end of the time slot to receive the reward.
Xiao Ming found that these mini-games are very simple, and he can complete any mini-game within a time slot. The key issue is how to arrange which mini-game to choose in each time slot to maximize the total reward.
[Input Description]
The first line of input contains a positive integer . This is both the number of time slots and the number of mini-games.
The second line contains positive integers. The positive integer is the completion deadline for the mini-game.
The third line contains positive integers. The positive integer is the completion reward for the mini-game.
[Output Description]
Output a line containing a positive integer, which is the maximum reward that can be obtained.
[Sample Input 1]
7
4 2 4 3 1 4 6
70 60 50 40 30 20 10
[Sample Output 1]
230
[Sample Explanation]
In the time slots, the 4th, 2nd, 3rd, 1st, 6th, 7th, and 5th mini-games can be arranged to be completed within the deadlines. Therefore, a total reward of can be obtained.
Reference Answer:
/*
* [GESP202309 Level 5] Skillfully Winning Prizes
* https://www.luogu.com.cn/problem/B3872
*/
# include
# include
using namespace std;
struct Game
{
int T;
int R;
};
Game games[505];
bool cmp(Game n1, Game n2)
{
return n1.R > n2.R;
}
int arr[505];// Record intermediate results, placing games in appropriate positions.
int main()
{
// 1. Store data
int n;
cin >> n;
for (int i = 0;i < n;i++)
{
cin >> games[i].T;
}
for (int i = 0;i < n;i++)
{
cin >> games[i].R;
}
// 2. Process data
int ans = 0;
// Sort rewards in descending order
sort(games, games + n, cmp);
for (int i = 0;i < n;i++)
{
int t = games[i].T;
int j = t;
while (arr[j] != 0)
{
j--;
}
if (j > 0)
{
arr[j] = games[i].R;
ans += arr[j];
}
}
// 3. Output result
cout << ans << endl;
return 0;
}
Day 15: Openjudge 2.6.1775_Herb Gathering
[Submit]
http://noi.openjudge.cn/ch0206/1775/
[Description]
Chen Chen is a talented and gifted child whose dream is to become the greatest physician in the world. To achieve this, he wants to apprentice with the most prestigious physician nearby. To assess his qualifications, the physician presents him with a challenge. The physician takes him to a cave filled with herbs and says, “Child, there are various herbs in this cave, each requiring some time to gather and each has its own value. I will give you a period of time during which you can gather some herbs. If you are a clever child, you should be able to maximize the total value of the herbs you gather.”
If you were Chen Chen, could you complete this task?
[Input]
The first line contains two integers and , representing the total time available for gathering herbs and the number of herbs in the cave. The next lines each include two integers between and (inclusive), representing the time required to gather a specific herb and the value of that herb.
[Output]
Output a single line containing an integer representing the maximum total value of herbs that can be gathered within the specified time.
[Sample Input]
70 3
71 100
69 1
1 2
[Sample Output]
3
[Source]
NOIP 2005
[Reference Program]
/*
* 1775: Herb Gathering
* http://noi.openjudge.cn/ch0206/1775/
*/
# include
# include
using namespace std;
int main()
{
int T, M;
cin >> T >> M;
vector time(M + 1);
vector value(M + 1);
for (int i = 1; i <= M; i++)
{
cin >> time[i] >> value[i];
}
// dp[j] represents the maximum value obtainable in time j.
vector dp(T + 1, 0);
for (int i = 1; i <= M; i++)
{
for (int j = T; j >= time[i]; j--)
{
dp[j] = max(dp[j], dp[j - time[i]] + value[i]);
}
}
cout << dp[T] << endl;
return 0;
}
Youth Programming Competition Exchange
The “Youth Programming Competition Exchange 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.
