资讯详情

第十一届蓝桥杯题目和解答(C++B组)省赛

题目

  • A门牌制作
  • B既约分数
  • C蛇形填数
  • D跑步锻炼
  • E七段码
  • F成绩统计
  • G回文日期
  • H子串分值
  • I平面切分
  • J字串排序

A门牌制作

小兰想为一条街的居民制作门牌号。这条街一共有 2020 住户,门牌号从 1 到 2020 编号。小蓝制作门牌的方法是先制作门牌 0 到 9 这些数字据需要将这些数字字符粘贴在门牌上,如门牌 1017 要依次粘贴字符 1、 0、 1、 7,即需要 1 个字符 0, 2个字符 1, 1 个字符 7。制作一切 1 到 2020 号门牌需要多少个字符? 2? 从1到2019,哪一个是2对应的?i,j,k,l 加一,最后在2020年加两个2。

#include<iostream> #include<cstring> #include<algorithm>  using namespace std;  int main() { 
          int res = 0;  int count = 0;  for(int i = 1; i <= 1;   i)   for(int j = 0; j <= 9;   j)    for(int k = 0; k <= 9;   k)     for(int l = 0; l <= 9;   l)     { 
              count  ;      if(i == 2)   res;      if(j == 2)   res;      if(k == 2)   res;      if(l == 2) ++ res; } for(int i = 2; i <= 2; ++ i) for(int k = 0; k <= 2; ++ k) for(int l = 0; l <= 9 && k !=2; ++ l) { 
           count ++; if(i == 2) ++ res; if(k == 2) ++ res; if(l == 2) ++ res; } for(int j = 1; j <= 9; ++ j) for(int k = 0; k <= 9; ++ k) for(int l = 0; l <= 9; ++ l) { 
           count ++; if(j == 2) ++ res; if(k == 2) ++ res; if(l == 2) ++ res; } for(int k = 1; k <= 9; ++ k) for(int l = 0; l <= 9; ++ l) { 
           count ++; if(k == 2) ++ res; if(l == 2) ++ res; } for(int l = 1; l <= 9; ++ l) { 
           count ++; if(l == 2) ++ res; } cout << res + 2 << endl; cout << count; } 
答案:624

B既约分数

如果一个分数的分子和分母的最大公约数是1,这个分数称为既约分数。例如,3/4 , 5/2 , 1/8 , 7/1都是既约分数。请问,有多少个既约分数,分子和分母都是1 到2020 之间的整数(包括1和2020)? 遍历所有的分数,如果分子和分母的最大公约数是1,res++。

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int gcd(int a, int b)
{ 
        
	return b ? gcd(b, a % b) : a;
}

int main()
{ 
        
	int res = 0;
	for(int i = 1; i <= 2020; ++ i)
		for(int j = 1; j <= 2020; ++ j)
			if(gcd(i, j) == 1)
				++ res;
	
	cout << res;	
} 
答案:2481215

C蛇形填数

如下图所示,小明用从1 开始的正整数“蛇形”填充无限大的矩阵

在这里插入图片描述 容易看出矩阵第二行第二列中的数是5。请你计算矩阵中第20 行第20 列的数是多少? 斜着看这个矩阵,可以看出每一斜行是递增,斜行的数量和正行的数量关系是m = 2 * (n - 1) ,m:斜行,n:正行。第n行n列的数在第m + 1斜行。所以n行n列的数就是前m斜行加上m + 1斜行中的一部分即n。

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int main()
{ 
        
	int res = 0;
	int n = 20;  ///行数 20 
	int m = 2 *(n - 1);  //斜着的行数 
	res = (1 + m) * m /2;
	res += n;
	cout << res;		
} 
答案:761

D跑步锻炼

小蓝每天都锻炼身体。正常情况下,小蓝每天跑 1 千米。如果某天是周一或者月初(1 日),为了激励自己,小蓝要跑 2 千米。如果同时是周一或月初,小蓝也是跑 2 千米。小蓝跑步已经坚持了很长时间,从 2000 年 1 月 1 日周六(含)到 2020 年10 月 1 日周四(含)。请问这段时间小蓝总共跑步多少千米? 1,3,5,7,8,10,12月 31天 4,6,9,11月 30天 闰年:366 天 , 2 月 29 天 平年:365 天 ,2 月 28 天

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int month = 1;
int week = 6;
int day = 1;
int res = 0; ///千米数 

bool isRunnian(int n)
{ 
        
	if((n % 4 == 0 && n % 100 != 0) || n % 400 == 0) return true;
	else return false;
}
int main()
{ 
        
	int near = 2000;
	int month;
	int day;
	int week = 6;
	int sum_day;
	int res = 0;
	
	while(near <= 2019)
	{ 
        
		month = 1;
		while(month <= 12)
		{ 
        
			day = 1;
			if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8|| month == 10 || month == 12)
				sum_day = 31;
			else if(month == 4 || month == 6 || month == 9 || month == 11)
				sum_day = 30;
			else if(isRunnian(near))
				sum_day = 29;
			else
				sum_day = 28;
			while(day <= sum_day)
			{ 
        
				++ res;
				if(day == 1 || week == 1)
				{ 
        
					++ res;
				}
				++ day;
				week = week % 7 + 1;
			}
			
			++ month;
		}
		++ near;
	}
	month = 1;
	near = 2020;
	while(month <= 9)
	{ 
        
		day = 1;
		if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
			sum_day = 31;
		else if(month == 4 || month == 6 || month == 9 || month == 11)
			sum_day = 30;
		else if(isRunnian(near))
			sum_day = 29;
		else
			sum_day = 28;
		
		while(day <= sum_day)
		{ 
        
			++ res;
			if(day == 1 || week == 1)
			{ 
        
				++ res;	
			}
			++ day;
			week = week % 7 + 1;
		}
		++ month;
	}
		
	cout << res + 2; 
} 
答案:8879

E七段码

小蓝要用七段码数码管来表示一种特殊的文字。

七段码上图给出了七段码数码管的一个图示,数码管中一共有7 段可以发光的二极管,分别标记为a, b, c, d, e, f, g。小蓝要选择一部分二极管(至少要有一个)发光来表达字符。在设计字符的表达时,要求所有发光的二极管是连成一片的。 例如:b 发光,其他二极管不发光可以用来表达一种字符。 例如:c 发光,其他二极管不发光可以用来表达一种字符。这种方案与上一行的方案可以用来表示不同的字符,尽管看上去比较相似。 例如:a, b, c, d, e 发光,f, g 不发光可以用来表达一种字符。 例如:b, f 发光,其他二极管不发光则不能用来表达一种字符,因为发光的二极管没有连成一片。 请问,小蓝可以用七段码数码管表达多少种不同的字符?

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
// A B C D E F G
// 0 1 2 3 4 5 6 
int list[20][2] = { 
        { 
        0,1},{ 
        0,5},{ 
        1,0},{ 
        1,6},{ 
        1,2},{ 
        2,1},{ 
        2,6},
{ 
        2,3},{ 
        3,2},{ 
        3,4},{ 
        4,3},{ 
        4,6},{ 
        4,5},{ 
        5,0},{ 
        5,6},{ 
        5,4},{ 
        6,5},
{ 
        6,1},{ 
        6,2},{ 
        6,4}};
int fa[7];

int init()
{ 
        
	for(int i = 0; i < 7; i ++)	fa[i] = i;
}

int find(int x)
{ 
        
	if(fa[x] == x)	return x;
	return find(fa[x]);
}

void Union(int x, int y)
{ 
        
	int x_root = find(x);
	int y_root = find(y);
	fa[y_root] = x_root;
}

int count_sameroot(int x)
{ 
        
	int ans = 0;
	for(int j = 0; j < 7; j ++)
		if(find(j) == x)
			ans ++;
	return ans;
}

int main()
{ 
        
	int ans = 0;	//计数不同字符 
	int map[7];		//7根灯管的亮暗情况 
	for(int i = 1; i <= 127; i ++) // 数码管的亮暗一共有2的8次方 - 1种 
	{ 
        
		int temp = i;	 
		int count = 0;
		for(int j = 0; j < 7; j ++)	//转换成2进制 
		{ 
        
			map[j] = temp % 2;
			if(map[j] == 1)	count ++;	//记录亮的数量 
			temp = temp / 2;
		}
		init();	//初始化 
		for(int q = 0; q < 7; q ++)
		{ 
        
			if(map[q] == 1)	//如果灯管亮 
			{ 
        
				for(int t = 0; t < 7; t ++)	//循环找到另一个不同的亮灯管 
				{ 
        	
					if(map[t] == 1 && t != q)	//如果找到了 
					{ 
        
						//判断两个灯管是否可以连接
						for(int j = 0; j < 20; j ++)
						{ 
        
							if(list[j][0] == t && list[j][1] == q)	Union(t, q);
							
						} 
					}
				}
			}
		}
		int max = 0;
		for(int j = 0; j < 7; j ++)
			if(max < count_sameroot(j))
				max = count_sameroot(j);
		if(count == max)	ans ++;	 
	}
	cout << ans;
}
答案:80

F成绩统计

小蓝给学生们组织了一场考试,卷面总分为 100 分,每个学生的得分都是 一个 0 到 100 的整数。 请计算这次考试的最高分、最低分和平均分。 输入的第一行包含一个整数n,表示考试人数 接下来n行,每行包含一个0至100的整数,表示一个学生的得分。 输出三行。 第一行包含一个整数,表示最高分。 第二行包含一个整数,表示最低分。 第三行包含一个实数,四舍五入保留正好两位小数,表示平均分。 7 80 92 56 74 88 99 10 99 10 71.29

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int main(){ 
        
	int n,m;
	double count = 0;
	int max = -1;
	int min = 101;
	
	cin >> n;
	int a = n;
	while(a --){ 
        
		cin >> m;
		if(m > max)	max = m;
		if(m < min)	min = m;
		count += m;
	}
	cout << max << endl;
	cout << min << endl;
	printf("%.2lf", count / n);
	
	return 0;
}

G回文日期

2020 年春节期间,有一个特殊的日期引起了大家的注意:2020年2月2日。因为如果将这个日期按“yyyymmdd” 的格式写成一个8 位数是20200202, 恰好是一个回文数。我们称这样的日期是回文日期。 有人表示20200202 是“千年一遇” 的特殊日子。对此小明很不认同,因为不到2年之后就是下一个回文日期:20211202 即2021年12月2日。 也有人表示20200202 并不仅仅是一个回文日期,还是一个ABABBABA型的回文日期。对此小明也不认同,因为大约100 年后就能遇到下一个ABABBABA 型的回文日期:21211212 即2121 年12 月12 日。算不上“千年一遇”,顶多算“千年两遇”。 给定一个8 位数的日期,请你计算该日期之后下一个回文日期和下一个ABABBABA型的回文日期各是哪一天。

输入包含一个八位整数N,表示日期。

输出两行,每行1 个八位数。第一行表示下一个回文日期,第二行表示下 一个ABABBABA 型的回文日期。输入包含一个八位整数N,表示日期。

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

bool isdate(int year, int month, int day)
{ 
        
	if(month >= 1 && month <= 12)
			if(month == 1 || month == 3 || month == 5 || 
				month == 7 || month == 8 || month == 10 || 
				month == 12 &&day >= 1 && day <= 31)
				return true;
			else if(month == 4 || month == 6 || month == 9 || 
					month == 11 && day >= 1 && day <= 30)
					return true;
			else 
				if(year % 4 == 0 && year %100 != 0 || (year % 400 == 0))
					if(day >= 1 && day <= 29)
						return true;
				else 
					if(day >= 
        标签: in5934b二极管

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台