您的位置:首页 > 编程语言 > C语言/C++

2016第七届蓝桥杯 B组 C/C++ 真题及题解

2018-03-25 00:06 471 查看
1. 煤球数目
有一堆煤球,堆成三角棱锥形。具体:
第一层放1个,
第二层3个(排列成三角形),
第三层6个(排列成三角形),
第四层10个(排列成三角形),
....
如果一共有100层,共有多少个煤球?

请填表示煤球总数目的数字。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
分析:递推,第i层煤球数为arr[i] = arr[i-1] + i;
           计算出每一层的数量然后全部加起来

代码:#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
int arr[1001];
int main()
{
arr[1] = 1;
int ans = 1;//遍历不经过第一层,所以提前加上第一层的一个
for(int i = 2;i<=100;i++){
arr[i] = arr[i-1] + i;
ans+=arr[i];
}
cout<<ans<<endl;
return 0;
}【答案】171700

2. 生日蜡烛
某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛。
现在算起来,他一共吹熄了236根蜡烛。
请问,他从多少岁开始过生日party的?

请填写他开始过生日party的年龄数。

注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
分析:枚举
代码:#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
int arr[1001];
int count(int i,int j){
int ans = 0;
for(int k = i;k<=j;k++){
ans+=k;
}
return ans;
}
int main()
{
for(int i = 1;i<= 100;i++){
for(int j = i;j<=100;j++){
//表示从i岁过到j岁
if(count(i,j) == 236){
cout<<i<<" "<<j<<endl;
return 0;
}
}
}
return 0;
}【答案】26
3. 凑算式


这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。

这个算式一共有多少种解法?
注意:你提交应该是个整数,不要填写任何多余的内容或说明性文字。
分析:枚举,使用next_permutation()可以自动进行全排列减少循环次数
代码:

4. 快速排序排序在各种场合经常被用到。
快速排序是十分常用的高效率的算法。

其思想是:先选一个“标尺”,
用它把整个队列过一遍筛子,
以保证:其左边的元素都不大于它,其右边的元素都不小于它。

这样,排序问题就被分割为两个子区间。
再分别对子区间排序就可以了。

下面的代码是一种实现,请分析并填写划线部分缺少的代码。

#include <stdio.h>

void swap(int a[], int i, int j)
{
int t = a[i];
a[i] = a[j];
a[j] = t;
}

int partition(int a[], int p, int r)
{
int i = p;
int j = r + 1;
int x = a[p];
while(1){
while(i<r && a[++i]<x);
while(a[--j]>x);
if(i>=j) break;
swap(a,i,j);
}
______________________;
return j;
}

void quicksort(int a[], int p, int r)
{
if(p<r){
int q = partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
}

int main()
{
int i;
int a[] = {5,13,6,24,2,8,19,27,6,12,1,17};
int N = 12;

quicksort(a, 0, N-1);

for(i=0; i<N; i++) printf("%d ", a[i]);
printf("\n");

return 0;
}
【答案】swap(a,p,j)

5. 抽签
X星球要派出一个5人组成的观察团前往W星。
其中:
A国最多可以派出4人。
B国最多可以派出2人。
C国最多可以派出2人。
....

那么最终派往W星的观察团会有多少种国别的不同组合呢?

下面的程序解决了这个问题。
数组a[] 中既是每个国家可以派出的最多的名额。
程序执行结果为:
DEFFF
CEFFF
CDFFF
CDEFF
CCFFF
CCEFF
CCDFF
CCDEF
BEFFF
BDFFF
BDEFF
BCFFF
BCEFF
BCDFF
BCDEF
....
(以下省略,总共101行)

#include <stdio.h>
#define N 6
#define M 5
#define BUF 1024

void f(int a[], int k, int m, char b[])
{
int i,j;

if(k==N){
b[M] =
4000
0;
if(m==0) printf("%s\n",b);
return;
}

for(i=0; i<=a[k]; i++){
for(j=0; j<i; j++) b[M-m+j] = k+'A';
______________________;  //填空位置
}
}
int main()
{
int  a
= {4,2,2,1,1,3};
char b[BUF];
f(a,0,M,b);
return 0;
}
分析:m表示剩余可派的人数
k控制A到F国派人
【答案】f(a,k+1,m-i,b)

6. 方格填数
如下的10个格子



填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)
一共有多少种可能的填数方案?

请填写表示方案数目的整数。

注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
分析:DFS搜索即可
代码:#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
using namespace std;
int d[3][4],num[10],ans = 0;
int dir[8][2]={{0,-1},{-1,0},{1,0},{0,1},{1,1},{-1,1},{1,-1},{-1,-1}};
int valid(int x,int y,int n){
for(int i =0;i<8;i++){
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if(tx>=0&&tx<3&&ty>=0&&ty<4){
if(d[tx][ty] == n-1||d[tx][ty] == n+1){
return false;
}
}
}
return true;
}
void dfs(int x,int y){//从上至下,从左至右
if(x == 2&&y == 3){//到达终点
ans++;
return;
}
if(y >= 4){//一行dfs完了,dfs下一行
dfs(x+1,0);
}
else{//继续遍历本行
//遍历每个数字
for(int i = 0;i<10;i++){
if(!num[i]&&valid(x,y,i)){
num[i] = 1;
d[x][y] = i;
dfs(x,y+1);
num[i] = 0;
d[x][y] = -2;
}
}
}
return;
}
int main(){
//初始化
for(int i =0;i<3;i++)
for(int j = 0;j<4;j++)
d[i][j] = -2;
memset(num,0,sizeof(num));
dfs(0,1);
cout<<ans<<endl;
return 0;
}【答案】1580

7. 剪邮票
如【图1.jpg】, 有12张连在一起的12生肖的邮票。
现在你要从中剪下5张来,要求必须是连着的。
(仅仅连接一个角不算相连)
比如,【图2.jpg】,【图3.jpg】中,粉红色所示部分就是合格的剪取。
请你计算,一共有多少种不同的剪取方法。






请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
分析:使用DFS枚举,然后用BFS判断枚举出的5个块是否相连
代码:#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
using namespace std;
struct node{
int x,y;
node(){}
node(int x,int y):x(x),y(y){}
}nd[10];
bool d[3][4],tempd[3][4];
int ans = 0;
int len = 0;//存放dfs找到邮票的位置
int dir[4][2]={-1,0,0,-1,1,0,0,1};
bool valid(int x,int y){
if(x<0||x>=3||y<0||y>=4) return false;
else return true;
}

bool bfs(){//bfs判断选出来的5块邮票是否连通
//不破坏原图,将图拷贝到tempd中进行处理
memcpy(tempd,d,sizeof(tempd));
queue<node> q;
q.push(nd[0]);
tempd[nd[0].x][nd[0].y] = 0;//已加入和不能加入的设置为0
int count = 1;//连通块的个数
while(!q.empty()){
node tempn = q.front(); q.pop();
for(int i = 0 ;i<4;i++){
int tempx = tempn.x + dir[i][0];
int tempy = tempn.y + dir[i][1];
if(valid(tempx,tempy)&&tempd[tempx][tempy] == 1){
tempd[tempx][tempy] = 0;
count++;
q.push(node(tempx,tempy));
}
}
}
return (count == 5);
}
void dfs(int x,int y,int hold){//dfs用来枚举,hold表示当前已经有的邮票数目
if(hold == 5){//找到五张就return
if(bfs()) ans++;
return;
}
if(x == 3) return;//越界则返回
for(int i = y;i<=4;i++){
if(i<4){//还在当前层
nd[len++] =node(x,i);
d[x][i] = 1;
dfs(x,i+1,hold+1);
len--;
d[x][i] = 0;
}
else{//到下一层
dfs(x+1,0,hold);
}
}
}

int main() {
memset(d,0,sizeof(d));
dfs(0,0,0);
cout<<ans<<endl;
return 0;
}【答案】116

8. 四平方和
四平方和定理,又称为拉格朗日定理:
每个正整数都可以表示为至多4个正整数的平方和。
如果把0包括进去,就正好可以表示为4个数的平方和。

比如:
5 = 0^2 + 0^2 + 1^2 + 2^2
7 = 1^2 + 1^2 + 1^2 + 2^2
(^符号表示乘方的意思)

对于一个给定的正整数,可能存在多种平方和的表示法。
要求你对4个数排序:
0 <= a <= b <= c <= d
并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输出第一个表示法

程序输入为一个正整数N (N<5000000)
要求输出4个非负整数,按从小到大排序,中间用空格分开

例如,输入:
5
则程序应该输出:
0 0 1 2

再例如,输入:
12
则程序应该输出:
0 2 2 2

再例如,输入:
773535
则程序应该输出:
1 1 267 838

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

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。
提交时,注意选择所期望的编译器类型。【分析】枚举+暴力求解法        由题意,不难想到思路:枚举a, b, c, d(满足0 <= a <= b <= c <= d),找符合条件 a*a+b*b+c*c+d*d=N 的解即可。        但需要注意数据规模:正整数N<5000000,因此要做好充分的程序优化。法一:由于a*a+b*b+c*c+d*d=N,故a, b, c, d每个数均不超过它们的算术平方根(也就是a*a<=N b*b<=N c*c<=N d*d<=N)。并且在求解过程中,若出现 a*a+b*b>N 或 a*a+b*b+c*c>N的情况,一定不合题意,不在继续枚举后面的数。       循环上述过程,直到找到第一组解为止。
分析:暴力枚举,三层循环,加上优化
代码:#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
using namespace std;
const int N =50;
int main(){
int n;
cin>>n;
int flag = 0;//找到答案后标为1
for(int i =0;i*i<=n;i++){
for(int j= i;j*j<=n;j++){
if(i*i+j*j>n) continue;
for(int k = j;k*k<=n;k++){
if(i*i+j*j+k*k>n) continue;
int a = sqrt(n-i*i-j*j-k*k);//关键!不使用第四层循环
if(i*i+j*j+k*k+a*a == n){
cout<<i<<" "<<j<<" "<<k<<" "<<a<<endl;
flag = 1;
break;
}
}
if(flag == 1) break;
}
if(flag == 1) break;
}
return 0;
}

9. 交换瓶子
有N个瓶子,编号 1 ~ N,放在架子上。
比如有5个瓶子:
2 1 3 5 4
要求每次拿起2个瓶子,交换它们的位置。
经过若干次后,使得瓶子的序号为:
1 2 3 4 5

对于这么简单的情况,显然,至少需要交换2次就可以复位。
如果瓶子更多呢?你可以通过编程来解决。

输入格式为两行:
第一行: 一个正整数N(N<10000), 表示瓶子的数目
第二行:N个正整数,用空格分开,表示瓶子目前的排列情况。

输出数据为一行一个正整数,表示至少交换多少次,才能完成排序。

例如,输入:
5
3 1 2 5 4

程序应该输出:
3

再例如,输入:
5
5 4 3 2 1

程序应该输出:
2

资源约定:
峰值内存消耗 < 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>
using namespace std;
const int N =10005;
int a
;
int main(){
int n,cnt = 0;
cin>>n;
for(int i = 1;i<=n;i++) cin>>a[i];
for(int i = 1;i<=n;i++){
if(a[i]!=i){
for(int j = i+1;j<=n;j++){
if(a[j] == i){
swap(a[i],a[j]);
cnt++;
break;
}
}
}
}
cout<<cnt<<endl;
return 0;
}

10. 最大比例
X星球的某个大奖赛设了M级奖励。每个级别的奖金是一个正整数。
并且,相邻的两个级别间的比例是个固定值。
也就是说:所有级别的奖金数构成了一个等比数列。比如:
16,24,36,54
其等比值为:3/2

现在,我们随机调查了一些获奖者的奖金数。
请你据此推算可能的最大的等比值。

输入格式:
第一行为数字 N (0<N<100),表示接下的一行包含N个正整数
第二行N个正整数Xi(Xi<1 000 000 000 000),用空格分开。每个整数表示调查到的某人的奖金数额

要求输出:
一个形如A/B的分数,要求A、B互质。表示可能的最大比例系数

测试数据保证了输入格式正确,并且最大比例是存在的。

例如,输入:
3
1250 200 32

程序应该输出:
25/4

再例如,输入:
4
3125 32 32 200

程序应该输出:
5/2

再例如,输入:
3
549755813888 524288 2

程序应该输出:
4/1

资源约定:
峰值内存消耗 < 256M
CPU消耗  < 3000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: 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>
using namespace std;
struct fraction {
long long fz, fm;//分子和分母
bool operator == (const fraction &x)const {
if (fm == x.fm&&fz == x.fz) return true;
else return false;
}
}frac[105];

long long num[105];

long long gcd(long long a, long long b) {
int t = 1;
while (t = a%b) {
a = b;
b = t;
}
return b;
}

//分数相除
fraction divd(fraction a, fraction b) {
if (a == b) return a;
long long temp;
//分子分母约分
temp = gcd(a.fm, b.fm);
a.fm /= temp; b.fm /= temp;
temp = gcd(a.fz, b.fz);
a.fz /= temp; b.fz /= temp;
//模拟除法
a.fz *= b.fm;
b.fz *= a.fm;
//大的放上面
if (a.fz > b.fz) {
a.fm = b.fz;
}
else {
a.fm = a.fz;
a.fz = b.fz;
}
return a;

}
int main() {
int n;
cin>>n;
//输入数据,并且将分数数组的分母全部初始化为1
for (int i = 0; i<n; i++) {
cin >> num[i];
frac[i].fm = 1;
}
sort(num, num + n);
//除重并且将数据存到分数数组中
int cnt = 0;
for (int i = 0; i<n; i++) {
if (num[i] != num[i + 1]) {
frac[cnt++].fz = num[i];
}
}
for (int j = cnt - 1; j; j--) {
for (int i = 0; i<j; i++) {
frac[i] = divd(frac[i], frac[i + 1]);
}
}
if (cnt == 1) cout << "1
a4b1
/1" << endl;
else cout << frac[0].fz << "/" << frac[0].fm << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: