蓝桥杯2015年省赛C/C++大学B组

  1. 奖券数目

有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利。
虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求。某抽奖活动的奖券号码是5位数(10000-99999),要求其中不要出现带“4”的号码,主办单位请你计算一下,如果任何两张奖券不重号,最多可发出奖券多少张。

请提交该数字(一个整数),不要写任何多余的内容或说明性文字。

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
bool check(int x){
	while(x){
		if(x % 10 == 4) return false;
		x /= 10;
	}
	return true;
}
int main(){
	int ans = 0;
	for(int i = 10000; i <= 99999; i++){
		if(check(i)) ans++;
	}
	cout << ans << endl;
	return 0;
}
//52488
  1. 星系炸弹

在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。
每个炸弹都可以设定多少天之后爆炸。
比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。
有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。

请填写该日期,格式为 yyyy-mm-dd 即4位年份2位月份2位日期。比如:2015-02-19
请严格按照格式书写。不能出现其它文字或符号。

思路
由于时间较近,直接用EXCEL就可以得到答案
在这里插入图片描述

  1. 三羊献瑞

观察下面的加法算式:

      祥 瑞 生 辉
  +   三 羊 献 瑞
-------------------
   三 羊 生 瑞 气

(如果有对齐问题,可以参看图片)
在这里插入图片描述

其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。

请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。

思路
可以分析三等于1,羊等于0,祥等于9,登限制条件去枚举,也可以直接暴力枚举,就是代码有点难写,可以结合退出来几个数字就可以用来优化代码,这里直接暴力枚举

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
/*
    a 祥  b 瑞  c 生  d 辉
    e 三  f 羊  g献  b瑞
   e三  f羊  c生  b瑞  h气
   a*1000 + b * 100 + c * 10 + d + e * 1000 + f * 100 + g * 10 + b
  = e * 10000 + f * 1000 + c * 100 + b * 10 + h 
*/	
int ans;
int main(){

	for(int a = 0; a <= 9; a++)
	for(int b = 0; b <= 9; b++)
	for(int c = 0; c <= 9; c++)
	for(int d = 0; d <= 9; d++)
	for(int e = 0; e <= 9; e++)
	for(int f = 0; f <= 9; f++)
	for(int g = 0; g <= 9; g++)
	for(int h = 0; h <= 9; h++)
	{
		if(a == b || a == c || a == d || a == e || a == f || a == g || a == h || b == c || b == d || b == e || b == f || b == g || b == h || c == d || c == e || c == f || c == g || c == h || d == e || d == f || d == g || d == h || e == f || e == g || e == h || f == g || f == h || g == h) continue;
		if(e == 0) continue;
		if( (a*1000 + b * 100 + c * 10 + d + e * 1000 + f * 100 + g * 10 + b ) == (e * 10000 + f * 1000 + c * 100 + b * 10 + h) )
		{
			ans = a*1000 + b * 100 + c * 10 + d;
			cout << ans << endl;
			ans = e * 1000 + f * 100 + g * 10 + b; 
			cout << ans << endl;
			ans = e * 10000 + f * 1000 + c * 100 + b * 10 + h;
			cout << "------------" << endl << ans << endl << endl;		
		}
	}
	return 0;
}
/*			9567
	答案	1085
			------------
			10652
*/
  1. 格子中输出

StringInGrid函数会在一个指定大小的格子中打印指定的字符串。
要求字符串在水平、垂直两个方向上都居中。
如果字符串太长,就截断。
如果不能恰好居中,可以稍稍偏左或者偏上一点。

下面的程序实现这个逻辑,请填写划线部分缺少的代码。

#include <stdio.h>
#include <string.h>

void StringInGrid(int width, int height, const char* s)
{
	int i,k;
	char buf[1000];
	strcpy(buf, s);
	if(strlen(s)>width-2) buf[width-2]=0;
	
	printf("+");
	for(i=0;i<width-2;i++) printf("-");
	printf("+\n");
	
	for(k=1; k<(height-1)/2;k++){
		printf("|");
		for(i=0;i<width-2;i++) printf(" ");
		printf("|\n");
	}
	
	printf("|");
	
	printf("%*s%s%*s",_____________________________________________);  //填空
//答案 printf("%*s%s%*s",(width - strlen(s) - 2) / 2, "", s, (width - strlen(s) - 2) / 2, "");            
	printf("|\n");
	
	for(k=(height-1)/2+1; k<height-1; k++){
		printf("|");
		for(i=0;i<width-2;i++) printf(" ");
		printf("|\n");
	}	
	
	printf("+");
	for(i=0;i<width-2;i++) printf("-");
	printf("+\n");	
}

int main()
{
	StringInGrid(20,6,"abcd1234");
	return 0;
}
对于题目中数据,应该输出:
+------------------+
|                  |
|     abcd1234     |
|                  |
|                  |
+------------------+

(如果出现对齐问题,参看【图1.jpg】

注意:只填写缺少的内容,不要书写任何题面已有代码或说明性文字。

  1. 九数组分数

1,2,3…9 这九个数字组成一个分数,其值恰好为1/3,如何组法?

下面的程序实现了该功能,请填写划线部分缺失的代码。

#include <stdio.h>

void test(int x[])
{
	int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];
	int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];
	
	if(a*3==b) printf("%d / %d\n", a, b);
}

void f(int x[], int k)
{
	int i,t;
	if(k>=9){
		test(x);
		return;
	}
	
	for(i=k; i<9; i++){
		{t=x[k]; x[k]=x[i]; x[i]=t;}
		f(x,k+1);
		_____________________________________________ // 填空处
		// 答案 {t=x[k]; x[k]=x[i]; x[i]=t;} //回溯
	}
}
	
int main()
{
	int x[] = {1,2,3,4,5,6,7,8,9};
	f(x,0);	
	return 0;
}

注意:只填写缺少的内容,不要书写任何题面已有代码或说明性文字。

  1. 加法变乘法

我们都知道:1+2+3+ … + 49 = 1225
现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015

比如:

1+2+3+...+10*11+12+...+27*28+29+...+49 = 2015

就是符合要求的答案。

请你寻找另外一个可能的答案,并把位置靠前的那个乘号左边的数字提交(对于示例,就是提交10)。

注意:需要你提交的是一个整数,不要填写任何多余的内容。
代码

#include <iostream>
using namespace std;
int sum(int i, int j){
	int sum = 0;
	for(i; i <= j; i++) sum += i;
	return sum;
}	
int main()
{
	for(int i = 2; i <= 48; i++){
		for(int j = i + 3; j <= 48; j++){
			int a = sum(1, i - 1);
			int b = sum(i + 2, j - 1);
			int c = sum(j + 2, 49);
			int x = i * (i + 1);
			int y = j * (j + 1);
			if(a + b + c + x + y == 2015) cout << i << endl;
			}
		}	
	return 0;
}
//10
//16
  1. 牌型种数

小明被劫持到X赌城,被迫与其他3人玩牌。
一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张。
这时,小明脑子里突然冒出一个问题:
如果不考虑花色,只考虑点数,也不考虑自己得到的牌的先后顺序,自己手里能拿到的初始牌型组合一共有多少种呢?

请填写该整数,不要填写任何多余的内容或说明文字。
思路
DFS搜索

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int ans;
void dfs(int dian, int sum, int pai){
	if(pai == 13){
		 ans++;
		return;
	}
	if(dian > 15) return ;
 	dfs(dian + 1, sum, pai);
	dfs(dian + 1, sum + dian, pai + 1);
	dfs(dian + 1, sum + dian * 2, pai + 2);
	dfs(dian + 1, sum + dian * 3, pai + 3);
	dfs(dian + 1, sum + dian * 4, pai + 4);
} 
int main()
{
	dfs(3, 0, 0);
	cout << ans << endl;
	return 0;
}
//3598180
  1. 移动距离

X星球居民小区的楼房全是一样的,并且按矩阵样式排列。其楼房的编号为1,2,3…
当排满一行时,从下一行相邻的楼往反方向排号。
比如:当小区排号宽度为6时,开始情形如下:

1 2 3 4 5 6
12 11 10 9 8 7
13 14 15 …

我们的问题是:已知了两个楼号m和n,需要求出它们之间的最短移动距离(不能斜线方向移动)

输入为3个整数w m n,空格分开,都在1到10000范围内
w为排号宽度,m,n为待计算的楼号。
要求输出一个整数,表示m n 两楼间最短移动距离。

例如:
用户输入:
6 8 2
则,程序应该输出:
4

再例如:
用户输入:
4 7 20
则,程序应该输出:
5

资源约定:
峰值内存消耗 < 256M
CPU消耗  < 1000ms

思路
根据题目给出的条件推出运算的公式
x为行 y为列

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int w, n, m;
int x1, y1, x2, y2; 
int main()
{
	cin >> w >> n >> m;
	x1 = n % w == 0 ? n / w : n / w + 1;
	x2 = m % w == 0 ? m / w : m / w + 1;
	if(x1 & 1) y1 = w - (x1 * w - n);
	else y1 = x1 * w - n + 1;
	if(x2 & 1) y2 = w - (x2 * w - m);
	else y2 = x2 * w - m + 1;
	int ans = abs(x1 - x2) + abs(y1 - y2);
	cout << ans << endl;
	return 0;
}

  1. 垒骰子

赌圣atm晚年迷恋上了垒骰子,就是把骰子一个垒在另一个上边,不能歪歪扭扭,要垒成方柱体。
经过长期观察,atm 发现了稳定骰子的奥秘:有些数字的面贴着会互相排斥!
我们先来规范一下骰子:1 的对面是 4,2 的对面是 5,3 的对面是 6。
假设有 m 组互斥现象,每组中的那两个数字的面紧贴在一起,骰子就不能稳定的垒起来。
atm想计算一下有多少种不同的可能的垒骰子方式。
两种垒骰子方式相同,当且仅当这两种方式中对应高度的骰子的对应数字的朝向都相同。
由于方案数可能过多,请输出模 10^9 + 7 的结果。

不要小看了 atm 的骰子数量哦~

「输入格式」
第一行两个整数 n m
n表示骰子数目
接下来 m 行,每行两个整数 a b ,表示 a 和 b 数字不能紧贴在一起。

「输出格式」
一行一个数,表示答案模 10^9 + 7 的结果。

「样例输入」
2 1
1 2

「样例输出」
544

「数据范围」
对于 30% 的数据:n <= 5
对于 60% 的数据:n <= 100
对于 100% 的数据:0 < n <= 10^9, m <= 36


资源约定:
峰值内存消耗 < 256M
CPU消耗  < 2000ms

思路
构造互斥矩阵,利用矩阵快速幂在logn时间级别得到答案

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
int n, m, a, b;
struct node{
	ll a[6][6];
	node(){
		for(int i = 0; i < 6; i++)
			for(int j = 0; j < 6; j++)
				a[i][j] = 1;
	}
};
int op[7] = {0, 4, 5, 6, 1, 2, 3};
node mul(node m1, node m2){
	node ans;
	for(int i = 0; i < 6; i++)
		for(int j = 0; j < 6; j++){
			ans.a[i][j] = 0;
			for(int k = 0; k < 6; k++)
				ans.a[i][j] = (ans.a[i][j] + m1.a[i][k] * m2.a[k][j]) % mod;
		}
	return ans;
}
node quickPow(node r, int k){
	node ans;//构造单位矩阵 
	for(int i = 0; i < 6; i++)
		for(int j = 0; j < 6; j++)
			if(i == j) ans.a[i][j] = 1;
			else ans.a[i][j] = 0;
	while(k){
		if(k & 1) ans = mul(ans, r);
		k >>= 1;
		r = mul(r, r);
	}
	return ans;
}
int main(){
	scanf("%d%d", &n, &m);
	node r; //构造互斥矩阵 
	for(int i = 1; i <= m; i++){
		scanf("%d%d", &a, &b);
		r.a[op[a] - 1][b - 1] = 0;
		r.a[op[b] - 1][a - 1] = 0;
	}
	node ansm = quickPow(r, n - 1);
	ll ans = 0;
	for(int i = 0; i < 6; i++)
		for(int j = 0; j < 6; j++)
			ans = (ans + ansm.a[i][j]) % mod;
	ll t = 1;
	ll temp = 4;
	int k = n;
	while(k){
		if(k & 1) t = (t * temp) % mod;
		k >>= 1;
		temp = (temp * temp) % mod;
	}
	ans = ans * t % mod;
	printf("%lld", ans);
	return 0;
} 
  1. 生命之树

在X森林里,上帝创建了生命之树。

他给每棵树的每个节点(叶子也称为一个节点)上,都标了一个整数,代表这个点的和谐值。
上帝要在这棵树内选出一个非空节点集S,使得对于S中的任意两个点a,b,都存在一个点列 {a, v1, v2, …, vk, b} 使得这个点列中的每个点都是S里面的元素,且序列中相邻两个点间有一条边相连。

在这个前提下,上帝要使得S中的点所对应的整数的和尽量大。
这个最大的和就是上帝给生命之树的评分。

经过atm的努力,他已经知道了上帝给每棵树上每个节点上的整数。但是由于 atm 不擅长计算,他不知道怎样有效的求评分。他需要你为他写一个程序来计算一棵树的分数。

「输入格式」
第一行一个整数 n 表示这棵树有 n 个节点。
第二行 n 个整数,依次表示每个节点的评分。
接下来 n-1 行,每行 2 个整数 u, v,表示存在一条 u 到 v 的边。由于这是一棵树,所以是不存在环的。

「输出格式」
输出一行一个数,表示上帝给这棵树的分数。

「样例输入」
5
1 -2 -3 4 5
4 2
3 1
1 2
2 5

「样例输出」
8

「数据范围」
对于 30% 的数据,n <= 10
对于 100% 的数据,0 < n <= 10^5, 每个节点的评分的绝对值不超过 10^6 。

资源约定:
峰值内存消耗 < 256M
CPU消耗  < 3000ms

思路
类似树形DP问题,任选一个点为根节点,遍历整棵树, 找到所有的连通图由于不存在环,也就不存在死循环,对于每个节点有选和不选两个情况,若这个点的值大于0,当然就是要选的,用ans记录下遍历过程中的最大值

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int len, h[N * 2], w[N], ne[N * 2], e[N * 2];
int n, a, b;
int f[N];
int ans;
void add(int a, int b){
	e[len] = b;
	ne[len] = h[a];
	h[a] = len++;
}
void dfs(int son, int fa){
	f[son] = w[son];
	for(int i = h[son]; ~i; i = ne[i]){
		int y = e[i];		
		if(y == fa) continue;
		dfs(y, son);
		if(f[y] > 0) f[son] += f[y];
	}
	ans = max(ans, f[son]);
}
int main(){
	memset(h, -1, sizeof h);
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%d", &w[i]);
	for(int i = 1; i < n; i++){
		scanf("%d%d", &a, &b);
		add(a, b);
		add(b, a);
	} 
	dfs(1, 0);
	printf("%d\n", ans);
	return 0;
}

发布了103 篇原创文章 · 获赞 203 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_45432665/article/details/104251272
今日推荐