0
点赞
收藏
分享

微信扫一扫

2013年第四届蓝桥杯试题(C/C++本科B组)

王传学 2022-09-16 阅读 130


1

大数学家高斯有个好习惯:无论如何都要记日记。



    他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210


    后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?



    高斯出生于:1777年4月30日。


    在高斯发现的一个重要定理的日记上标注着:5343,因此可算出那天是:1791年12月15日。


    高斯获得博士学位的那天日记上标着:8113   


    请你算出高斯获得博士学位的年月日。


提交答案的格式是:yyyy-mm-dd, 例如:1980-03-21


请严格按照格式,通过浏览器提交答案。


注意:只提交这个日期,不要写其它附加内容,比如:说明性的文字。

答案:1799-07-16

#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 = 1777;
int month,day;
int sum = 8113 - 245 - 1;//到这一年年底的时候剩余的时间,减1是因为出生的时候也算一天
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;
}

2

小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了。




    有一次,老师出的题目是:36 x 495 = ?




    他却给抄成了:396 x 45 = ?




    但结果却很戏剧性,他的答案竟然是对的!!




    因为 36 * 495 = 396 * 45 = 17820




    类似这样的巧合情况可能还有很多,比如:27 * 594 = 297 * 54




    假设 a b c d e 代表1~9不同的5个数字(注意是各不相同的数字,且不含0)




    能满足形如: ab * cde = adb * ce 这样的算式一共有多少种呢?






请你利用计算机的优势寻找所有的可能,并回答不同算式的种类数。




满足乘法交换律的算式计为不同的种类,所以答案肯定是个偶数。






答案直接通过浏览器提交。


注意:只提交一个表示最终统计种类数的数字,不要提交解答过程或其它多余的内容。

答案:142(简单回溯一下就行)

#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 visit[10] = {0};
int a[5];
int sum = 0;

void dfs(int cur)
{
if(cur == 5)
{
int sum1 = a[0] * 10 + a[1];
int sum2 = a[2] * 100 + a[3] * 10 + a[4];
int sum3 = a[0] * 100 + a[3] * 10 + a[1];
int sum4 = a[2] * 10 + a[4];
if(sum1 * sum2 == sum3 * sum4)
{
sum++;
}
return;
}
for(int i=1; i<=9; i++)
{
if(!visit[i])
{
visit[i] = 1;
a[cur] = i;
dfs(cur+1);
visit[i] = 0;
}
}
}

int main()
{
dfs(0);
printf("%d\n",sum);
return 0;
}


3

小明刚刚看完电影《第39级台阶》,离开电影院的时候,他数了数礼堂前的台阶数,恰好是39级!


    站在台阶前,他突然又想着一个问题:


    如果我每一步只能迈上1个或2个台阶。先迈左脚,然后左右交替,最后一步是迈右脚,也就是说一共要走偶数步。那么,上完39级台阶,有多少种不同的上法呢?


    请你利用计算机的优势,帮助小明寻找答案。


要求提交的是一个整数。
注意:不要提交解答过程,或其它的辅助说明文字。

答案:51167078

#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 dp[40][2];//第一维表示第几节台阶,第二维0表示踏上第i阶台阶是是左脚,1表示是右脚

int main()
{
dp[1][0] = 1;
dp[1][1] = 0;
dp[2][0] = 1;
dp[2][1] = 1;
for(int i=3; i<=39; i++)
{
dp[i][1] = dp[i-1][0] + dp[i-2][0];
dp[i][0] = dp[i-1][1] + dp[i-2][1];
}
printf("%d\n",dp[39][1]);
return 0;
}

4

黄金分割数0.61803... 是个无理数,这个常数十分重要,在许多工程问题中会出现。有时需要把这个数字求得很精确。


    对于某些精密工程,常数的精度很重要。也许你听说过哈勃太空望远镜,它首次升空后就发现了一处人工加工错误,对那样一个庞然大物,其实只是镜面加工时有比头发丝还细许多倍的一处错误而已,却使它成了“近视眼”!!




    言归正传,我们如何求得黄金分割数的尽可能精确的值呢?有许多方法。


    比较简单的一种是用连分数:


                  1
    黄金数 = ---------------------
                        1
             1 + -----------------
                          1
                 1 + -------------
                            1
                     1 + ---------
                          1 + ...


                           


    这个连分数计算的“层数”越多,它的值越接近黄金分割数。


    请你利用这一特性,求出黄金分割数的足够精确值,要求四舍五入到小数点后100位。


    小数点后3位的值为:0.618
    小数点后4位的值为:0.6180
    小数点后5位的值为:0.61803
    小数点后7位的值为:0.6180340
   (注意尾部的0,不能忽略)


你的任务是:写出精确到小数点后100位精度的黄金分割值。


注意:尾数的四舍五入! 尾数是0也要保留!


显然答案是一个小数,其小数点后有100位数字,请通过浏览器直接提交该数字。
注意:不要提交解答过程,或其它辅助说明类的内容。

答案:

5

    如下的代码判断 needle_start指向的串是否为haystack_start指向的串的前缀,如不是,则返回NULL。


    比如:"abcd1234" 就包含了 "abc" 为前缀

char* prefix(char* haystack_start, char* needle_start)
{
char* haystack = haystack_start;
char* needle = needle_start;



while(*haystack && *needle){
if(______________________________) return NULL; //填空位置
}

if(*needle) return NULL;

return haystack_start;
}



请分析代码逻辑,并推测划线处的代码,通过网页提交。
注意:仅把缺少的代码作为答案,千万不要填写多余的代码、符号或说明文字!!

答案:*(haystack++) != *(needle++)

6

一般的排序有许多经典算法,如快速排序、希尔排序等。


    但实际应用时,经常会或多或少有一些特殊的要求。我们没必要套用那些经典算法,可以根据实际情况建立更好的解法。


    比如,对一个整型数组中的数字进行分类排序:


    使得负数都靠左端,正数都靠右端,0在中部。注意问题的特点是:负数区域和正数区域内并不要求有序。可以利用这个特点通过1次线性扫描就结束战斗!!


    以下的程序实现了该目标。


    其中x指向待排序的整型数组,len是数组的长度。

void sort3p(int *x, int len)
{
int p = 0;
int left = 0;
int right = len-1;

while(p<=right){
if(x[p]<0){
int t = x[left];
x[left] = x[p];
x[p] = t;
left++;
p++;
}
else if(x[p]>0){
int t = x[right];
x[right] = x[p];
x[p] = t;
right--;
}
else{
__________________________; 填空位置
}
}

}


如果给定数组:
25,18,-2,0,16,-5,33,21,0,19,-16,25,-3,0
则排序后为:
-3,-2,-16,-5,0,0,0,21,19,33,25,16,18,25



请分析代码逻辑,并推测划线处的代码,通过网页提交
注意:仅把缺少的代码作为答案,千万不要填写多余的代码、符号或说明文字!!

答案:p++

剩下的题本博客都有,单独写出来了

举报

相关推荐

0 条评论