2015年第六届蓝桥杯试题(C/C++本科B组)
2016-03-09 21:39
791 查看
1
有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利。
虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求。某抽奖活动的奖券号码是5位数(10-99),要求其中不要出现带“4”的号码,主办单位请你计算一下,如果任何两张奖券不重号,最多可发出奖券多少张。
请提交该数字(一个整数),不要写任何多余的内容或说明性文字。
答案:第一位不取0和4,剩下四位不取4, 8*9*9*9*9
2
星系炸弹
在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。
每个炸弹都可以设定多少天之后爆炸。
比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。
有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。
请填写该日期,格式为 yyyy-mm-dd即4位年份2位月份2位日期。比如:2015-02-19
请严格按照格式书写。不能出现其它文字或符号。
答案:2017-08-05
3
三羊献瑞
观察下面的加法算式:
祥瑞生辉
+ 三羊献瑞
-
三羊生瑞气
(如果有对齐问题,可以参看【图1.jpg】)
其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。
请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。
答案:1085
4
格子中输出
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;
}
对于题目中数据,应该输出:
+------------------+
| |
| abcd1234 |
| |
| |
+------------------+
(如果出现对齐问题,参看【图1.jpg】)
注意:只填写缺少的内容,不要书写任何题面已有代码或说明性文字。
答案:(width-strlen(s)-2)/2,"",s,(width-strlen(s)-1)/2,""(什么鬼,完全看不懂啊)
5
九数组分数
1,2,3...9 这九个数字组成一个分数,其值恰好为1/3,如何组法?
下面的程序实现了该功能,请填写划线部分缺失的代码。
1 #include <stdio.h>
2
3void test(int x[])
4 {
5 int a = x[0] *1
+ x[1] *100 + x[2] *10
+ x[3];
6 int b = x[4] *10
+ x[5] *1 + x[6] *100
+ x[7] *10 + x[8];
7
8 if (a *3 == b) printf("%d
/ %d\n", a, b);
9 }
10
11void f(int x[],int
k)
12 {
13 int i, t;
14 if (k >=9)
15 {
16 test(x);
17 return;
18 }
19
20 for (i = k; i<9; i++)
21 {
22 {t = x[k];x[k] = x[i]; x[i] = t; }
23 f(x, k +1);
24 _____________________________________________//填空处
25 }
26 }
27
28int main()
29 {
30 int x[] = {1,2,3,4,5,6,7,8,9
};
31 f(x,0);
32 return0;
33 }
注意:只填写缺少的内容,不要书写任何题面已有代码或说明性文字。
答案:简单回溯,就是全排列代码,把顺序倒过来就行
t = x[k];x[k] = x[i]; x[i] = t;
还以为写代码了,写了一遍,贴上来吧
加法变乘法
我们都知道:1+2+3+ ... + 49 = 1225
现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015
比如:
1+2+3+...+10*11+12+...+27*28+29+...+49 = 2015
就是符合要求的答案。
请你寻找另外一个可能的答案,并把位置靠前的那个乘号左边的数字提交(对于示例,就是提交10)。
注意:需要你提交的是一个整数,不要填写任何多余的内容。
答案:16
枚举乘号的位置即可
49个数中间有48个符号
7
小明被劫持到X赌城,被迫与其他3人玩牌。
一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张。
这时,小明脑子里突然冒出一个问题:
如果不考虑花色,只考虑点数,也不考虑自己得到的牌的先后顺序,自己手里能拿到的初始牌型组合一共有多少种呢?
请填写该整数,不要填写任何多余的内容或说明文字。
答案:3598180(每种牌的个数是0,1,2,3,4)枚举一下即可
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
using namespace std;
int ans = 0;
void dfs(int cur,int sum)
{
if(sum > 13)
{
return;
}
if(cur == 13)
{
if(sum == 13)
{
ans++;
}
return;
}
for(int i=0; i<=4; i++)
{
dfs(cur+1,sum+i);
}
}
int main()
{
dfs(0,0);
printf("%d\n",ans);
return 0;
}
8
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到10范围内
w为排号宽度,m,n为待计算的楼号。
要求输出一个整数,表示m n两楼间最短移动距离。
例如:
用户输入:
6 8 2
则,程序应该输出:
4
再例如:
用户输入:
4 7 20
则,程序应该输出:
5
资源约定:
峰值内存消耗 < 256M
CPU消耗 < 1ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...”的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意:
只使用ANSI C/ANSI C++标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意:
所有依赖的函数必须明确地在源文件中 #include <xxx>,不能通过工程设置而省略常用头文件。
提交时,注意选择所期望的编译器类型。
答案:求每个数的坐标,横纵坐标减一下取绝对值加起来就行
有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利。
虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求。某抽奖活动的奖券号码是5位数(10-99),要求其中不要出现带“4”的号码,主办单位请你计算一下,如果任何两张奖券不重号,最多可发出奖券多少张。
请提交该数字(一个整数),不要写任何多余的内容或说明性文字。
答案:第一位不取0和4,剩下四位不取4, 8*9*9*9*9
2
星系炸弹
在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。
每个炸弹都可以设定多少天之后爆炸。
比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。
有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。
请填写该日期,格式为 yyyy-mm-dd即4位年份2位月份2位日期。比如:2015-02-19
请严格按照格式书写。不能出现其它文字或符号。
答案:2017-08-05
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x3f3f3f3f;//1061109567 typedef long long ll; using namespace std; int mon[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; bool isleap(int year) { if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true; return false; } int main() { int year = 2014; int month,day; int sum = 1000 - 52;//到这一年年底的时候剩余的时间 int sum1; while(true) { if(isleap(year+1)) { sum1 = 366; } else { sum1 = 365; } if(sum < sum1) { year++; break; } else { year++; sum -= sum1; } } if(isleap(year)) { mon[2]++; } for(int i=1; i<=12; i++) { if(sum > mon[i]) { sum -= mon[i]; } else { month = i; break; } } day = sum; printf("%d %d %d\n",year,month,day); return 0; }
3
三羊献瑞
观察下面的加法算式:
祥瑞生辉
+ 三羊献瑞
-
三羊生瑞气
(如果有对齐问题,可以参看【图1.jpg】)
其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。
请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。
答案:1085
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x3f3f3f3f;//1061109567 typedef long long ll; using namespace std; int a[10]; int visit[10] = {0}; void dfs(int cur) { if(cur == 8) { int sum = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3]; int sum1 = a[4] * 1000 + a[5] * 100 + a[6] * 10 + a[1]; int sum2 = a[4] * 10000 + a[5] * 1000 + a[2] * 100 + a[1] * 10 + a[7]; if(sum + sum1 == sum2) { printf("%d %d %d\n",sum,sum1,sum2); } return; } for(int i=0; i<10; i++) { if(i == 0 && cur == 0) continue; if(i == 0 && cur == 4) continue; if(!visit[i]) { visit[i] = 1; a[cur] = i; dfs(cur+1); visit[i] = 0; } } } int main() { dfs(0); return 0; }
4
格子中输出
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;
}
对于题目中数据,应该输出:
+------------------+
| |
| abcd1234 |
| |
| |
+------------------+
(如果出现对齐问题,参看【图1.jpg】)
注意:只填写缺少的内容,不要书写任何题面已有代码或说明性文字。
答案:(width-strlen(s)-2)/2,"",s,(width-strlen(s)-1)/2,""(什么鬼,完全看不懂啊)
5
九数组分数
1,2,3...9 这九个数字组成一个分数,其值恰好为1/3,如何组法?
下面的程序实现了该功能,请填写划线部分缺失的代码。
1 #include <stdio.h>
2
3void test(int x[])
4 {
5 int a = x[0] *1
+ x[1] *100 + x[2] *10
+ x[3];
6 int b = x[4] *10
+ x[5] *1 + x[6] *100
+ x[7] *10 + x[8];
7
8 if (a *3 == b) printf("%d
/ %d\n", a, b);
9 }
10
11void f(int x[],int
k)
12 {
13 int i, t;
14 if (k >=9)
15 {
16 test(x);
17 return;
18 }
19
20 for (i = k; i<9; i++)
21 {
22 {t = x[k];x[k] = x[i]; x[i] = t; }
23 f(x, k +1);
24 _____________________________________________//填空处
25 }
26 }
27
28int main()
29 {
30 int x[] = {1,2,3,4,5,6,7,8,9
};
31 f(x,0);
32 return0;
33 }
注意:只填写缺少的内容,不要书写任何题面已有代码或说明性文字。
答案:简单回溯,就是全排列代码,把顺序倒过来就行
t = x[k];x[k] = x[i]; x[i] = t;
还以为写代码了,写了一遍,贴上来吧
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x3f3f3f3f;//1061109567 typedef long long ll; using namespace std; int a[10]; int visit[10] = {0}; void dfs(int cur) { if(cur == 9) { int sum = a[0] * 10000 + a[1] * 1000 + a[2] * 100 + a[3] * 10 + a[4]; int sum1 = a[5] * 1000 + a[6] * 100 + a[7] * 10 + a[8]; if(sum % sum1 == 0 && sum / sum1 == 3) { printf("%d %d\n",sum,sum1); } return; } for(int i=0; i<10; i++) { if(i == 0 && cur == 0) continue; if(i == 0 && cur == 4) continue; if(!visit[i]) { visit[i] = 1; a[cur] = i; dfs(cur+1); visit[i] = 0; } } } int main() { dfs(0); return 0; }6
加法变乘法
我们都知道:1+2+3+ ... + 49 = 1225
现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015
比如:
1+2+3+...+10*11+12+...+27*28+29+...+49 = 2015
就是符合要求的答案。
请你寻找另外一个可能的答案,并把位置靠前的那个乘号左边的数字提交(对于示例,就是提交10)。
注意:需要你提交的是一个整数,不要填写任何多余的内容。
答案:16
枚举乘号的位置即可
49个数中间有48个符号
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x3f3f3f3f;//1061109567 typedef long long ll; using namespace std; int main() { for(int i=1; i<=46; i++) { for(int j=i+2; j<=48; j++) { int sum = 0; for(int k=1; k<i; k++) { sum += k; } sum += i * (i+1); for(int k=i+2; k<j; k++) { sum += k; } sum += j * (j+1); for(int k=j+2; k<=49; k++) { sum += k; } if(sum == 2015) { printf("%d %d\n",i,j); } } } }
7
小明被劫持到X赌城,被迫与其他3人玩牌。
一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张。
这时,小明脑子里突然冒出一个问题:
如果不考虑花色,只考虑点数,也不考虑自己得到的牌的先后顺序,自己手里能拿到的初始牌型组合一共有多少种呢?
请填写该整数,不要填写任何多余的内容或说明文字。
答案:3598180(每种牌的个数是0,1,2,3,4)枚举一下即可
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
using namespace std;
int ans = 0;
void dfs(int cur,int sum)
{
if(sum > 13)
{
return;
}
if(cur == 13)
{
if(sum == 13)
{
ans++;
}
return;
}
for(int i=0; i<=4; i++)
{
dfs(cur+1,sum+i);
}
}
int main()
{
dfs(0,0);
printf("%d\n",ans);
return 0;
}
8
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到10范围内
w为排号宽度,m,n为待计算的楼号。
要求输出一个整数,表示m n两楼间最短移动距离。
例如:
用户输入:
6 8 2
则,程序应该输出:
4
再例如:
用户输入:
4 7 20
则,程序应该输出:
5
资源约定:
峰值内存消耗 < 256M
CPU消耗 < 1ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...”的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意:
只使用ANSI C/ANSI C++标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意:
所有依赖的函数必须明确地在源文件中 #include <xxx>,不能通过工程设置而省略常用头文件。
提交时,注意选择所期望的编译器类型。
答案:求每个数的坐标,横纵坐标减一下取绝对值加起来就行
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x3f3f3f3f;//1061109567 typedef long long ll; using namespace std; int main() { int w,m,n; while(scanf("%d%d%d",&w,&m,&n) != EOF) { int a = m / w; if(m % w == 0)//每个数的横纵坐标都是从0开始,整除横坐标减1 a -= 1; int b = n / w; if(n % w== 0) b -= 1; int c,d; if(a % 2 == 1) { c = abs(m - w*(a+1)); } else { c = m - (w * a + 1); } if(b % 2 == 1) { d = abs(n-w*(b+1)); } else { d = n - (w * b + 1); } int sum = abs(a-b) + abs(c-d); printf("%d\n",sum); } return 0; }
相关文章推荐
- 第2周项目2-就拿胖子说事
- c++上机报告1-1
- C++的异常处理
- c语言:利用静态变量static,输出1到5的阶乘值
- C++ vector变量等导致内存泄露问题的解决方法
- CAF(C++ actor framework)(序列化之结构体,任意嵌套STL)(一)
- 判断任意数据是否奇偶数
- 装饰模式(Decorator)C++实现
- C++11 lambda表达式
- c++多线程编程(三)
- c++多线程编程(二)
- c++多线程编程(一)
- C++中友元函数
- c++11 可变参数模板
- C语言实现C++面向对象语言多态特性
- 第2周项目1
- c语言 谭浩强 一维数组内放10个学生成绩 全局变量写一个函数 最高分、最低分、平均分
- C++多态
- C语言之位运算
- C/C++面试题