[Blue Bridge Cup Sprint] The 11th Provincial Competition of the Blue Bridge Cup Group C++b Real Questions-Fill in the Blank Questions

Table of contents

Test Question A: Door Plate Making

Problem-solving ideas:

Answer:

Test Question B: Calculating Fractions

Problem-solving ideas:

Answer:

Test Question C: Snake Number Filling

Problem-solving ideas:

Answer:

Test Question D: Running Training

Problem-solving ideas:

Answer:

Question E: Seven-segment code

Problem-solving ideas:

Answer:

Write at the end:


Test Question A: Door Plate Making

Xiaolan wants to make house numbers for the residents of a street.

There are a total of 2020 residents on this street, and the house numbers are numbered from 1 to 2020.

The way Xiaolan makes the house number is to make the numbers from 0 to 9 first,

Finally, paste the characters on the door plate as needed,

For example, house number 1017 needs to paste characters 1, 0, 1, 7 in sequence,

That is, 1 character 0, 2 characters 1, and 1 character 7 are required.

May I ask how many characters 2 are needed to make all the house numbers from 1 to 2020?

Problem-solving ideas:

Just enumerate directly:

#include <iostream>
using namespace std;

int main()
{
	int cnt = 0;
	for (int i = 0; i <= 2020; i++) {
		int tmp = i;
		while (tmp) {
			if (tmp % 10 == 2) {
				cnt++;
			}
			tmp /= 10;
		}
	}
	cout << cnt << endl;
	return 0;
}

Answer:

624

Test Question B: Calculating Fractions

A fraction is called a reduced fraction if the greatest common divisor of its numerator and denominator is 1.

For example: 3 / 4 , 5 / 2 , 1 / 8 , 7 / 1 are all approximate fractions.

Excuse me, how many approximate fractions are there, the numerator and denominator are both integers between 1 and 2020 (including 1 and 2020)?

Problem-solving ideas:

The agreed score is actually an agreed score,

One of the characteristics of the agreed fraction is that the greatest common divisor of the numerator and denominator is 1.

#include <iostream>
using namespace std;

//找最大公约数
int gcd(int a, int b) {
	while (b) {
		int c = a % b;
		a = b;
		b = c;
	}
	if (a == 1) return a;
}

int main()
{
	int cnt = 0;
	for (int i = 1; i <= 2020; i++) {
		for (int j = 1; j <= 2020; j++) {
			//如果最大公约数是1,证明是既约分数
			if (gcd(i, j) == 1) {
				cnt++;
			}
		}
	}
	cout << cnt << endl;
	return 0;
}

Answer:

2481215

Test Question C: Snake Number Filling

As shown in the figure below, Xiaoming fills the infinite matrix with positive integers starting from 1 in a "snake shape".

 It is easy to see that the number in the second row and second column of the matrix is ​​5.

Please calculate the number in row 20 and column 20 of the matrix?

Problem-solving ideas:

My method is to find the law, which is easy to see:

1,5,13 ......

The next number is greater than the previous number (i - 1) * 4

#include <iostream>
using namespace std;

int main()
{
	int res = 1;
	for (int i = 2; i <= 20; i++) {
		res += (i - 1) * 4;
	}
	cout << res << endl;
	return 0;
}

Answer:

761

Test Question D: Running Training

Xiaolan exercises every day.

Under normal circumstances, Xiaolan runs 1 kilometer every day.

If a certain day falls on a Monday or the beginning of the month (1st), in order to motivate herself, Xiaolan will run 2 kilometers.

If it is Monday or the beginning of the month at the same time, Xiaolan will also run 2 kilometers.

Xiaolan has been running for a long time,

From Saturday 1 January 2000 (inclusive) to Thursday 1 October 2020 (inclusive).

How many kilometers did Xiaolan run in total during this period?

Problem-solving ideas:

This is a classic date problem,

The best thing to do is to remember the template for date questions,

Let's calculate this question until 2019, and finally deal with the 10 months of 2020:

#include <iostream>
using namespace std;

//打表
int day[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

bool is_leap(int year) {
	return (year % 4 == 0 && year % 100 || year % 400 == 0);
}

int get_day(int year, int month) {
	if (is_leap(year) && month == 2) return day[month] + 1;
	else return day[month];
}

int main()
{
	int cnt = 0, res = 0;

	//2000~2019
	for (int i = 2000; i <= 2019; i++) {
		for (int j = 1; j <= 12; j++) {
			for (int k = 1; k <= get_day(i, j); k++) {
				//我们用0~6表示周一到周日,因为2000.1.1是周六,所以+5
				int weekday = (cnt + 5) % 7;
				if (k == 1 || weekday == 0) res += 2;
				else res++;
				cnt++;
			}
		}
	}

	//2020.1.1~2020.9.30
	for (int i = 1; i <= 9; i++) {
		for (int j = 1; j <= get_day(2020, i); j++) {
			int weekday = (cnt + 5) % 7;
			if (j == 1 || weekday == 0) res += 2;
			else res++;
			cnt++;
		}
	}

	//2020.10.1
	res += 2;
	cout << res << endl;
	return 0;
}

You must pay attention to the details, otherwise it is easy to make mistakes.

Answer:

8879

Question E: Seven-segment code

Xiaolan wants to use a seven-segment digital tube to represent a special character.

Seven-segment code The figure above shows an illustration of the seven-segment code digital tube,

There are a total of 7 LEDs that can light up in the digital tube, marked as a, b, c, d, e, f, g respectively.

Xiaolan needs to choose a part of diodes (at least one) to emit light to express characters.

When designing the expression of characters, it is required that all light-emitting diodes be connected together.

For example: b emits light, and other diodes do not emit light, which can be used to express a character.

For example: c emits light, and other diodes do not emit light, which can be used to express a character.

This scheme can be used to represent different characters than the scheme in the previous line, although it looks similar.

For example: a, b, c, d, e are illuminated, f, g are not illuminated can be used to express a character.

For example: b, f are illuminated, and other diodes cannot be used to express a character if they do not emit light, because the light-emitting diodes are not connected together.

Excuse me, how many different characters can Xiaolan express with the seven-segment digital tube?

Problem-solving ideas:

This question uses dfs + and check set,

The embarrassing thing is that I don't know how to search and set, so I can only move the answer, I hope it can be helpful to you:

Code handling: code reference

#include <bits/stdc++.h>

using namespace std;
const int N = 10;
int ans;//统计结果
int p[N];//存放并查集中,根信息的数组
bool st[N];//记录DFS搜索的状态
int e[N][N];//记录七段码灯管彼此连通的的信息,e[i[[j]表示i和j的连通与否

//补全并查集的核心函数
int find(int x)
{
	if(p[x] != x) p[x] = find(p[x]);
	return p[x];
}

//补全DFS函数
void dfs(int u)
{
	//设置递归结束的边界!
	if(u == 8)
	{
		//并查集的初始化
		for (int i = 1; i <= 7; i ++) p[i] = i;
		
		//枚举这些被操作过灯管,检查连通情况
		for(int i = 1; i  <= 7; i++)
			for(int j = 1; j <= 7;j++)
			{
				//i是点亮了。j是点亮了。i和j之间是直接联系的
				if(st[i] && st[j] && e[i][j])
				{
					//放到一个连通块中
					p[find(i)] = find(j);
				}
			}
			
		
		int cnt = 0;
		for(int i = 1; i <= 7;i++)
			//当前灯管是亮的,而且形成一个连通块的了
			if(st[i] && p[i] == i) cnt++;

		if(cnt == 1) ans ++;
		
		return ;
	}
	
	//递归环节
	st[u] = 1;//打开当前灯管的情况
	dfs(u+1);
	
	st[u] = 0;
	dfs(u+1);//关闭当前灯管的情况
	
}

int main()
{
	//初始化1到7号,各个灯管,直接连通的情况
	e[1][2] = e[1][6] = 1;//意思就是,代表a号灯管的的1,和代表b的2号灯管连通,和代表f的6号灯管连通
	e[2][1] = e[2][3] = e[2][7] = 1;
	e[3][2] = e[3][7] = e[3][4] = 1;
	e[4][3] = e[4][5] = 1;
	e[5][4] = e[5][7] = e[5][6] = 1;
	e[6][1] = e[6][7] = e[6][5] = 1;
	e[7][2] = e[7][3] = e[7][5] = e[7][6] = 1;
	
	//从1号灯管开始DFS
	dfs(1);
	
	cout << ans << endl;
	return 0;
}

Answer:

80

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/129852188