第四届蓝桥杯软件类省赛真题 C/C++ 本科 - B

   题目标题: 高斯日记

    大数学家高斯有个好习惯:无论如何都要记日记。
    他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210
    后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?
    高斯出生于:1777年4月30日。
    在高斯发现的一个重要定理的日记上标注着:5343,因此可算出那天是:1791年12月15日。
    高斯获得博士学位的那天日记上标着:8113  
    请你算出高斯获得博士学位的年月日。

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

    求日期的其实几乎就这样的套路了。

   

    大概可以算出来是在二十几年后,所以代码也只能针对这道题,注意生下来那天开始就算第一天、

    然后我是用大概的日期,输入看看是不是符合天数

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

#define N 3010

bool tt[N];
int sg[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

void judge()
{
    for(int i = 1599; i <= 2999; i ++)
    {
        if( i % 4 == 0 && i % 100 != 0 ||(i % 400 == 0))
            tt[i] = true;
    }
}

int main()
{
    int yy, mm, dd;

    while(~scanf("%d%d%d",&yy,&mm,&dd))
    {
        memset(tt, false, sizeof(tt));
        judge();
        int ans = 245;
        for(int i = 1778; i <= yy - 1; i ++)
        {
            ans+=365;
            if(tt[i])
                ans++;
        }
        for(int i = 1; i < mm; i ++)
        {
            ans +=sg[i];
        }
        ans += dd;

        if(tt[yy] && mm >= 3)
        {
            ans ++;
        }
        printf("%d\n",ans + 1);
    }
}

/***

1791 12 15
1778 4 30
1799 7 16

**/


 马虎的算式


    小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了。
    有一次,老师出的题目是: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 这样的算式一共有多少种呢?
    请你利用计算机的优势寻找所有的可能,并回答不同算式的种类数。
    满足乘法交换律的算式计为不同的种类,所以答案肯定是个偶数。

    暴力:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main()
{
    int cnt = 0;
    for(int a = 1; a<= 9; a ++)
    {
        for(int b = 1; b <= 9; b ++)
        {
            for(int c = 1; c <= 9; c ++)
            {
                for(int d = 1; d <= 9; d ++)
                {
                    for(int e = 1; e <= 9; e ++)
                    {
                        if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e)
                        {
                            if((a*10 + b) * (c*100+d*10+e) == (a*100+d*10+b )*(c*10+e))
                                cnt ++;
                        }
                    }
                }
            }
        }
    }
    printf("%d\n",cnt);
}

第39级台阶


    小明刚刚看完电影《第39级台阶》,离开电影院的时候,他数了数礼堂前的台阶数,恰好是39级!
    站在台阶前,他突然又想着一个问题:
    如果我每一步只能迈上1个或2个台阶。先迈左脚,然后左右交替,最后一步是迈右脚,也就是说一共要走偶数步。那么,上完39级台阶,有多少种不同的上法呢?
    请你利用计算机的优势,帮助小明寻找答案。
    要求提交的是一个整数。

   

   简单的dp,一开始我是用递推的、、

   

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

#include <stdio.h>

int c[50][2];//0为左脚,1为右脚。。

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

 黄金连分数


    黄金分割数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位数字,请通过浏览器直接提交该数字。



前缀判断


    如下的代码判断 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++)

   

错误票据

某涉密单位下发了某种票据,并要在年终全部收回。
每张票据有唯一的ID号。全年所有票据的ID号是连续的,但ID的开始数码是随机选定的。
因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成了某个ID断号,另外一个ID重号。
你的任务是通过编程,找出断号的ID和重号的ID。
假设断号不可能发生在最大和最小号。
要求程序首先输入一个整数N(N<100)表示后面数据行数。
接着读入N行数据。
每行数据长度不等,是用空格分开的若干个(不大于100个)正整数(不大于100000)
每个整数代表一个ID号。

要求程序输出1行,含两个整数m n,用空格分隔。
其中,m表示断号ID,n表示重号ID

例如:
用户输入:
2
5 6 8 11 9
10 12 9

则程序输出:
7 9

再例如:
用户输入:
6
164 178 108 109 180 155 141 159 104 182 179 118 137 184 115 124 125 129 168 196
172 189 127 107 112 192 103 131 133 169 158
128 102 110 148 139 157 140 195 197
185 152 135 106 123 173 122 136 174 191 145 116 151 143 175 120 161 134 162 190
149 138 142 146 199 126 165 156 153 193 144 166 170 121 171 132 101 194 187 188
113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119

则程序输出:
105 120

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

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。

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


简单模拟:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

char tt[1010];
int aa[110];

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        getchar();
        int minn = 110;
        int maxn = 0;
        for(int j = 0; j < n; j ++)
        {
            memset(tt, 0, sizeof(tt));
            gets(tt);
            int len = strlen(tt);
            int k = 0;
            bool flag = true;
            tt[len] = ' ';
            for(int i = 0; i <= len; i ++)
            {
                if(tt[i] <= '9' && tt[i] >= '0')
                {
                    if(flag)
                    {
                        k = k*10 + tt[i] -'0';
                    }
                    else
                    {
                        k = tt[i] -'0';
                        flag = true;
                    }
                }
                else if(tt[i] == ' ')
                {
                    flag = false;
                    aa[k] ++;
                    minn = min(minn, k);
                    maxn = max(maxn, k);
                    k = 0;
                }

            }
        }
        int ans1, ans2;
        for(int i = minn ; i <= maxn; i ++)
        {
            if(aa[i] == 0)
                ans1 = i;
            else if(aa[i] == 2)
                ans2 = i;
        }
        printf("%d %d\n",ans1,ans2);
    }

}


翻硬币


    小明正在玩一个“翻硬币”的游戏。
    桌上放着排成一排的若干硬币。我们用 * 表示正面,用 o 表示反面(是小写字母,不是零)。
    比如,可能情形是:**oo***oooo
    如果同时翻转左边的两个硬币,则变为:oooo***oooo
    现在小明的问题是:如果已知了初始状态和要达到的目标状态,每次只能同时翻转相邻的两个硬币,那么对特定的局面,最少要翻动多少次呢?
    我们约定:把翻动相邻的两个硬币叫做一步操作,那么要求:
 
程序输入:
两行等长的字符串,分别表示初始状态和要达到的目标状态。每行的长度<1000

程序输出:
一个整数,表示最小操作步数

例如:
用户输入:
**********
o****o****

程序应该输出:
5

再例如:
用户输入:
*o**o***o***
*o***o**o***

程序应该输出:
1

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

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


    要注意的是,题目中的数据一定是可以解的,即不会出现奇数个不匹配的两个串。(应该是这样的),用贪心解决、

   

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

#define N 1010

char a[N],b[N];

int hash[N];

int main()
{
    while(~scanf("%s%s",a,b))
    {
        int len = strlen(a);
        for(int i = 0; i < len; i ++)
        {
            if(a[i] == b[i])
                hash[i] = 0;
            else
                hash[i] = 1;
        }
        int ans = 0;
        int flag = -1;
        for(int i = 0; i < len; i ++)
        {
            if(hash[i])
            {
                if(flag == -1)
                    flag = i;
                else
                {
                    ans += i - flag;
                    flag = -1;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


后面两题,一个搜索,但是我不会,另外一个 题目都看不懂、、

 



 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值