[Blue Bridge Cup Sprint] Blue Bridge Cup 11th Provincial Competition C++b Group Real Questions-Programming Questions

Table of contents

Question F: Grade Statistics

Problem-solving ideas:

code:

Question G: Palindromic Dates

Problem-solving ideas:

code:

Question H: String Scores

Problem-solving ideas:

code:

 Question I: Plane Segmentation

Problem-solving ideas:

code:

Test Question J: String Sorting

Problem-solving ideas:

Write at the end:


Question F: Grade Statistics

【Problem Description】

Xiaolan organizes an exam for the students, the total score of the paper is 100 points,

Each student's score is an integer from 0 to 100.

A score of at least 60 is called a pass.

A score of at least 85 is considered excellent.

Please calculate the pass rate and excellent rate, expressed as a percentage, and the part before the percentage sign is rounded to an integer.

【Input format】

The first line of input contains an integer n, indicating the number of people taking the exam.

Next n lines, each line contains an integer from 0 to 100, representing a student's score.

【Output format】

Output two lines, each with a percentage, indicating the passing rate and the excellent rate respectively.

The part before the percent sign is rounded to an integer.

【Sample input】

7
80
92
56
74
88
100
0

【Sample output】

71%
43%

Problem-solving ideas:

It's a simple mock question, the first question should be done carefully, there is no problem.

code:

#include <iostream>
using namespace std;

int main() {
	int n;
	cin >> n;
	double a = 0, b = 0, score;
	for (int i = 0; i < n; i++) {
		cin >> score;
		if (score >= 60) a++;
		if (score >= 85) b++;
	}
    //%.0f能够自动四舍五入
	printf("%.0f%% %.0f%%", (a / n) * 100, (b / n) * 100);
	return 0;
}

Question G: Palindromic Dates

【Problem Description】

During the Spring Festival of 2020, a special date has attracted everyone's attention: February 2, 2020.

Because if this date is written as an 8-digit number in the format of "yyyymmdd", it is 20200202,

Exactly a palindromic number. We call such dates palindromic dates.

Some people said that 20200202 is a special day of "once in a thousand years".

Xiao Ming disagrees with this, because the next palindrome date is less than 2 years later: 20211202, which is December 2, 2021.

Some people also said that 20200202 is not just a palindrome date, but also an ABABBABA type palindrome date.

Xiao Ming also disagrees with this:

Because the next palindrome date of ABABBABA type can be encountered in about 100 years: 21211212 is December 12, 2121

It's not "once in a thousand years", at most it's "once in a thousand years".

Given an 8-digit date,

Please calculate which day is the next palindrome date and the next ABABBABA type palindrome date after this date.

【Input format】

The input contains an eight-digit integer N representing the date.

【Output format】

Output two lines of 1 octet each.

The first row represents the next palindrome date,

The second row represents the next palindromic date of type ABABBABA.

【Sample input】

20200202

【Sample output】

20211202
21211212

[Evaluation use case scale and agreement]

For all evaluation cases, 10000101 ≤ N ≤ 89991231, guaranteeing that N is an 8-digit representation of a valid date.

Problem-solving ideas:

The idea of ​​this question is to enumerate the dates after that, and then judge whether they meet the requirements of the question.

According to the data return, we need to enumerate 1000~10000 years.

code:

#include <iostream>
using namespace std;

int mon[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

//20211202
bool check1(int date) {
	int year = date / 10000;
	int month = date / 100 % 100;
	int day = date % 100;

	if (month > 12) return false;

	if (month == 2 
	&& ((year % 4 == 0 && year % 100) || year % 400 == 0) 
	&& day > mon[month] + 1) return false;

	if (month != 2 && day > mon[month]) return false;

	return true;
}

//21211212
bool check2(int date) {
	int year = date / 10000;
	int month = date / 100 % 100;
	int day = date % 100;

	if (month > 12) return false;

	if (month == 2 
	&& ((year % 4 == 0 && year % 100) || year % 400 == 0) 
	&& day > mon[month] + 1) return false;

	if (month != 2 && day > mon[month]) return false;

	if (day != month) return false;

	return true;
}

int main() {
	int date1;
	cin >> date1;
	bool flag1 = false, flag2 = false;
	for (int i = 1000; i < 10000; i++) {
		int date = i, x = i;
		//将date翻转接上原来的date,就能让该日期变成一个回文串
		for (int i = 0; i < 4; i++) {
			date = date * 10 + x % 10;
			x /= 10;
		}
		//判断翻转之后该日期是否合法(题目第一个要求)
		if (!flag1 && date > date1 && check1(date)) {
			flag1 = true;
			cout << date << endl;
		}
		//判断翻转之后该日期是否合法以及是否是 ABABBABA 型
		if (!flag2 && date > date1 && check2(date)) {
			flag2 = true;
			cout << date << endl;
		}

		if (flag1 && flag2) break;
	}
	return 0;
}

Question H: String Scores

【Problem Description】

For a string S, we define the score f (S) of S as the number of distinct characters that appear in S.

For example, f("aba") = 2, f("abc") = 3, f("aaa") = 1.

Now given a string S[0..n − 1] (of length n),

Please calculate the sum of f (S [ i..j ]) for all non-empty substrings S [ i..j ] (0 ≤ i ≤ j < n) of S

【Input format】

Enter a line containing a string S consisting of lowercase letters.

【Output format】

Output an integer representing the answer.

【Sample input】

ababc

【Sample output】

28

【Example description】

子串    f值
a        1
ab       2
aba      2
abab     2
ababc    3
 b       1
 ba		 2
 bab	 2
 babc 	 3
   a	 1
   ab	 2
   abc	 3
    b	 1
    bc	 2
     c	 1

[Evaluation use case scale and agreement]

1 ≤ n ≤ 10 for 20% of evaluation cases;

1 ≤ n ≤ 100 for 40% of the evaluation cases;

1 ≤ n ≤ 1000 for 50% of the evaluation cases;

For 60% of the evaluation cases, 1 ≤ n ≤ 10000;

1 ≤ n ≤ 100000 for all evaluation cases.

Problem-solving ideas:

The idea I use here is to use the STL,

The characteristics of set, set can automatically help us to deduplicate,

Then we just need to enumerate the strings and count them.

code:

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

int main() {
	string s;
	cin >> s;
	int sum = 0;
	for (int i = 0; i < s.size(); i++) {
		set<char> p;
		for (int j = i; j < s.size(); j++) {
			p.insert(s[j]);
			sum += p.size();//去重之后的size就是字母的种类
		}
	}
	cout << sum << endl;
	return 0;
}

 Question I: Plane Segmentation

【Problem Description】

There are N straight lines on the plane, and the i-th straight line is y = Ai · x + Bi.

Calculate how these lines divide the plane into several parts.

【Input format】

The first line contains an integer N.

The following N lines each contain two integers Ai; Bi.

【Output format】

An integer representing the answer.

【Sample input】

3
1 1
2 2
3 3

【Sample output】

6

[Evaluation use case scale and agreement]

For 50% of the evaluation cases, 1 ≤ N ≤ 4, −10 ≤ Ai; Bi ≤ 10.

For all evaluation cases, 1 ≤ N ≤ 1000, −100000 ≤ Ai; Bi ≤ 100000.

Problem-solving ideas:

Mathematics scum said that it really can't be done. . . woo woo woo. . .

code:

Code reference:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;

using namespace std;
const int N = 100010;
string s;
int pre[N],ne[N];
int a[27];

int main()
{
	cin >>s;
	s = "0" + s;
	
	//获取字符串长度
	int len = s.length();
	
	//有步初始化,我直接声明的全局变量,是可以不用的,为了和后面操作想呼应,就写完整吧
	for(int i = 0; i < 27;i++) a[i] = 0;
	
	//找前一个相同字符的下标
	for(int i = 1; i < len;i++)
	{
		//将'a'变成偏移量,获得
		int index = s[i] - 'a';
		pre[i] = a[index];
		a[index] = i;
	}
	
	
	for(int i = 0; i < 27;i++) a[i] = len;
	
	//找下一个相同字符的下标
	for(int i = len - 1; i >= 1;i--)
	{
		int index = s[i] - 'a';
		ne[i] = a[index];
		a[index] = i;
	}
	
	LL ans = 0;
	
	for(int i = 1; i <len;i++)
		ans += (LL)(i - pre[i]) * (ne[i] - i);
		
	cout << ans << endl;
	
	return 0;
}

Test Question J: String Sorting

Problem-solving ideas:

Don't look, I can't even do the last question,

Of course this question can't be done. . .

Write at the end:

The above is the content of this article, thank you for reading.

If you like this article, please like and comment, and write down your opinions.

If you want to learn programming with me, you might as well follow me, we will learn and grow together.

I will output more high-quality content in the future, welcome to watch. 

Guess you like

Origin blog.csdn.net/Locky136/article/details/129874506