2015第六届蓝桥杯 A组 C/C++ 真题及题解

1. (3')方程整数解
方程: a^2 + b^2 + c^2 = 1000
这个方程有整数解吗?有:a,b,c=6,8,30 就是一组解。
你能算出另一组合适的解吗?
请填写该解中最小的数字。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

分析:枚举

代码:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
const int N =100005;
int d[N],sum[N],n,k;
int main(){
	for(int i =1;i*i<1000;i++){
		for(int j =i;j*j<1000;j++){
			int k = sqrt(1000-i*i-j*j);
			if(i*i+j*j+k*k == 1000&&k>j){
				cout<<i<<" "<<j<<" "<<k<<endl;
				
			}
		}
	}
	return 0;
}
/*输出:6 8 30
		10 18 24*/ 

【答案】

10 18 24


2. (5')星系炸弹

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

请严格按照格式书写。不能出现其它文字或符号。

分析:日期问题

代码:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
const int N =100005;
int y = 2014;
int m = 11;
int d = 9;
int mt[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},
				{0,31,29,31,30,31,30,31,31,30,31,30,31}};
bool leap(int t){
	return y%400 == 0||(y%4==0&&y%100!=0);
}
int process(int num){
	while(num){
		d++;
		num--;
		int flag;
		if(leap(y)) flag = 1;
		else flag = 0;
		if(d>mt[flag][m]){
			d = 1;
			m++;
			if(m>12){
				m=1;
				y++;
			} 
		}
	}
	cout<<y<<"-"<<m<<"-"<<d;
}
int main(){
	process(1000);
	return 0;
}
//输出:2017-8-5		

【答案】2017-08-05 


3. (9')奇妙的数字
小明发现了一个奇妙的数字。它的平方和立方正好把0~9的10个数字每个用且只用了一次。
你能猜出这个数字是多少吗?

请填写该数字,不要填写任何多余的内容。

分析:数位分离,枚举即可

代码:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
int num[10];//数字是否使用过的标记
void process(int n){
	while(n){
		num[n%10] = 1;
		n/=10;
	}
}

bool valid(){
	for(int i =0;i<10;i++) if(num[i]==0) return false;
	return true;
}

int main(){
	for(int i =1;i<10000;i++){
		memset(num,0,sizeof(num));
		process(i*i);process(i*i*i);
		if(valid()){
			cout<<i<<endl;
			return 0;
		}
	}
	return 0;
}
//输出:69	

【答案】69

4. (11')格子中输出
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("|\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;  
}  
对于题目中数据,应该输出:

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

分析:

【答案】(width-2-strlen(s))/2," ",s,((width-2-strlen(s))%2==0)?(width-2-strlen(s))/2:(width-2-strlen(s))/2+1," "


5. (15')九数组分数

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);  
        _____________________________________________ // 填空处  
    }  
}  
      
int main()  
{  
    int x[] = {1,2,3,4,5,6,7,8,9};  
    f(x,0);   
    return 0;  
}  

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

分析:所填部分为回溯思想的体现

【答案】t=x[k]; x[k]=x[i]; x[i]=t


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

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

分析:dfs,枚举出每种情况

代码:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
int card[13] = {4,4,4,4,4,4,4,4,4,4,4,4,4};
int ans = 0;
void dfs(int totalcard,int cardno){//totalcard表示当前持有的手牌数,cardno表示下一张要拿的手牌 
	if(totalcard == 13){
		ans++;
		return;
	}
	for(int i = cardno;i<13;i++){
		if(card[i]){//还有手牌可以拿 
			card[i]--;
			dfs(totalcard+1,i);
			card[i]++;//回溯 
		}
	}
}
int main(){
	dfs(0,0);
	cout<<ans<<endl;
	return 0;
}
//输出:3598180	

【答案】3598180


7. (21')手链样式
小明有3颗红珊瑚,4颗白珊瑚,5颗黄玛瑙。
他想用它们串成一圈作为手链,送给女朋友。
现在小明想知道:如果考虑手链可以随意转动或翻转,一共可以有多少不同的组合样式呢?

请你提交该整数。不要填写任何多余的内容或说明性的文字。


分析:因为可以随意转动或翻转所以将字符串扩大为原来两倍,

例如abcd和cdab,将cdab扩大两倍,cdabcdab中找得到abcd

则abcd和cdab就是同一种情况,再使用next_permutation()函数自动枚举全排列

代码:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
string str = "aaabbbbccccc";
int ans = 0;
vector<string> v;
int main() {
	do {
		int flag = 1;
		vector<string>::iterator it;
		for (it = v.begin(); it != v.end(); it++) {
			//遍历每个字符串寻找是否重复
			if ((*it).find(str, 0) != string::npos) {
				flag = 0;
				break;
			}
		}
		if (flag) {
			ans++;
			string tempstr = str + str;//任意转动
			v.push_back(tempstr);
			reverse(tempstr.begin(), tempstr.end());//任意翻转 
			v.push_back(tempstr);
		}
	} while (next_permutation(str.begin(), str.end()));
	cout << ans << endl;
	return 0;
}

【答案】1170


8. (13')饮料换购
乐羊羊饮料厂正在举办一次促销优惠活动。乐羊羊C型饮料,凭3个瓶盖可以再换一瓶C型饮料,并且可以一直循环下去(但不允许暂借或赊账)。
请你计算一下,如果小明不浪费瓶盖,尽量地参加活动,那么,对于他初始买入的n瓶饮料,最后他一共能喝到多少瓶饮料。

输入:一个整数n,表示开始购买的饮料数量(0<n<10000)
输出:一个整数,表示实际得到的饮料数

例如:
用户输入:
100
程序应该输出:
149

用户输入:
101
程序应该输出:
151

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

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。

提交时,注意选择所期望的编译器类型。

分析:模拟即可

代码:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
int process(int num){
	int sum = 0;
	if(num<3) return num; 
	while(num>=3){
		sum+=(num-num%3);
		int temp  = num%3;
		num/=3; num+=temp;
		if(num<3) sum+=num;
	}
	return sum;
}
int main(){
	int n;
	cin>>n;
	cout<<process(n);
	return 0;
}


9. (25')垒骰子
赌圣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

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。

提交时,注意选择所期望的编译器类型。

分析:快速矩阵幂

代码:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
const int opps[] = {0,1,2,3,4,5,6};
struct mat{
	int r,c;//行数与列数
	ll d[7][7];
	mat(){}
	mat(ll x){//初始化为6行6列的单位x矩阵 
		r=c=6;
		memset(d,0,sizeof(d));
		for(int i = 1;i<7;i++) d[i][i] = x;
	}
	mat(int nr,int nc,ll x){
		r = nr;
		c = nc;
		for(int i =1;i<=r;i++){
			for(int j = 1;j<=c;j++){
				d[i][j] = x;
			}
		}
	}
	mat operator * (const mat &x){
		mat res(r,x.c,0);
		for(int i =1;i<=r;i++){
			for(int j = 1;j<=x.c;j++){
				for(int k =1;k<=c;k++){
					res.d[i][j] += d[i][k]*x.d[k][j]; 
				}
			}
		}
		return res;
	}
	mat operator %(const int &x){
		for(int i =1;i<=r;i++){
			for(int j =1;j<=c;j++){
				d[i][j]%=x;
			}
		}
		return (*this);
	}
};
template <class Type>
Type fpow(Type x,int n){
	Type res(1);
	while(n){
		if(n&1) res=res*x%MOD;
		x = x*x%MOD;
		n>>=1;
	}
	return res;
}
int main() {
	int n,m;
	cin>>n>>m;
	mat M(6,6,1);
	for(int i =1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		M.d[x][opps[y]] = 0;
		M.d[y][opps[x]] = 0;
	}
	M = fpow(M,n-1);
	ll ans = 0,four = 4;
	for(int i =1;i<=6;i++){
		for(int j =1;j<=6;j++){
			ans = (ans + M.d[i][j])%MOD;
		}
	}
	ans = ans * fpow(four,n)%MOD;
	cout<<ans<<endl;
	return 0;
}



10. (31')灾后重建
Pear市一共有N(<=50000)个居民点,居民点之间有M(<=200000)条双向道路相连。这些居民点两两之间都可以通过双向道路到达。这种情况一直持续到最近,一次严重的地震毁坏了全部M条道路。
震后,Pear打算修复其中一些道路,修理第i条道路需要Pi的时间。不过,Pear并不打算让全部的点连通,而是选择一些标号特殊的点让他们连通。
Pear有Q(<=50000)次询问,每次询问,他会选择所有编号在[l,r]之间,并且 编号 mod K  = C 的点,修理一些路使得它们连通。由于所有道路的修理可以同时开工,所以完成修理的时间取决于花费时间最长的一条路,即涉及到的道路中Pi的最大值。
你能帮助Pear计算出每次询问时需要花费的最少时间么?这里询问是独立的,也就是上一个询问里的修理计划并没有付诸行动。

【输入格式】
第一行三个正整数N、M、Q,含义如题面所述。
接下来M行,每行三个正整数Xi、Yi、Pi,表示一条连接Xi和Yi的双向道路,修复需要Pi的时间。可能有自环,可能有重边。1<=Pi<=1000000。
接下来Q行,每行四个正整数Li、Ri、Ki、Ci,表示这次询问的点是[Li,Ri]区间中所有编号Mod Ki=Ci的点。保证参与询问的点至少有两个。

【输出格式】
输出Q行,每行一个正整数表示对应询问的答案。

【样例输入】
7 10 4
1 3 10
2 6 9
4 1 5
3 7 4
3 6 9
1 5 8
2 7 4
3 2 10
1 7 6
7 6 9
1 7 1 0
1 7 3 1
2 5 1 0
3 7 2 1

【样例输出】
9
6
8
8

【数据范围】
对于20%的数据,N,M,Q<=30
对于40%的数据,N,M,Q<=2000
对于100%的数据,N<=50000,M<=2*10^5,Q<=50000. Pi<=10^6. Li,Ri,Ki均在[1,N]范围内,Ci在[0,对应询问的Ki)范围内。

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

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。

提交时,注意选择所期望的编译器类型。


10. 生命之树
在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

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。
提交时,注意选择所期望的编译器类型。

分析:树形dp

代码:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N = 100005;
int res = -1000,v[N],dp[N],vis[N];
vector<int> e[N];
void dfs(int s){
	vis[s] = 1;
	dp[s] = v[s];
	for(int i =0;i<e[s].size();i++){
		if(!vis[e[s][i]]){
			dfs(e[s][i]);
			if(dp[e[s][i]]>0){
				dp[s] +=dp[e[s][i]];
			}
		}
	}
	res = max(dp[s],res);
}
int main() {
	int n;cin>>n;
	for(int i =1;i<=n;i++) cin>>v[i];
	for(int i =1;i<n;i++){
		int u,v;
		cin>>u>>v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	dfs(1);
	cout<<res;
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值