第十四届蓝桥杯第三期模拟赛 C/C++ B组 原题与详解

  • Post author:
  • Post category:其他


1、找最小全为字母的16进制数。

2、求一个数的26进制并用A-Z字母表示

3、日期相等

4、乘积方案数

5、最大连通块

6、求星期几

7、范围覆盖点数

8、清理水草

9、滑行距离

10、序号最小值

1、找最小全为字母的16进制数。

请找到一个大于 2022 的最小数,这个数转换成十六进制之后,所有的数位(不含前导 0)都为字母(A 到 F)。请将这个数的十进制形式作为答案提交。

答案:2730

#include <stdio.h>
int main(){
    int i;
    char ch;
    for(i=2023;;i++){
        int temp=i;
        while(temp!=0){
            ch=temp%16+'0';  //取余超过9,说明大于A,输出
            if(ch<='9')break;
            temp=temp/16;  //每一位都要满足条件
        }
        if(temp==0)break;
    }
    printf("%d",i);
    return 0;
}

#include<iostream>
using namespace std;  //如果没有定义std,在输出的时候就需要使用std::cout;否则直接用cout。
bool check_vaild(int x)   // bool是C++的关键字
{
    while (x) 
    {
        if (x % 16 >= 10 && x % 16 <= 15)
            ;//当满足条件,我们什么也不用做,继续循环即可
        else 
        {
            return false;  //false和true是C++的关键字
        }
        x /= 16; // 循环判断每一位
    }
    return true;
}
int main() 
{
    for (int i = 2023; ; i++) 
    {
        if (check_vaild(i)) 
        {
            // std::cout << i << "\n"; 、//直接输出换行也可以
            // std::cout << i << std::endl; //endl相当于换行
            // std::cout << i ; //不适用endl就相当于没有换行。
            cout<<i<<endl;
            break; //找到就停止即可
        }
    }
 
    return 0;
}
//注意:C语言没有bool、false、true关键字。如果需要使用则使用 头文件#include<stdbool.h>  
//或者使用
//#define bool char
//#define true 1
//#define false 0

2、求一个数的26进制并用A-Z字母表示

在 Excel 中,列的名称使用英文字母的组合。前 26 列用一个字母,依次为 A 到 Z,接下来 26*26 列使用两个字母的组合,依次为 AA 到 ZZ。请问第 2022 列的名称是什么?

答案:BYT

推测法:

// int main(){
//     int a;
//     a=2022-26-26*26-26*26-26*25;
//     //A-Z  26个数
//     //AA-ZZ 26*26个数

//     //AAA-AZZ 26*26个数
//     //BBB-BYY  26*25个数
//     //BBB-BZZ 26*26个数

//     //AAA-ZZZ  26*26*26个数
    

//     // Z Y X W V U      T 

//     printf("%d",a);
// }

取余法:

//等同于求2022的26进制
#include <stdio.h>
int main(){
    int num=2022;
    int a[5]={0},i=0;
    while(num>0){
        a[i++]=num%26;  //对26取余,把余数存放到数组中
        num/=26;  // 循环每一位不断取余
    }
    for(int j=i-1;j>=0;j--){ //从后往前循环,
        printf("%c",'A'+a[j]-1);    
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
int get(int x, int y, int z) //该条件为 百位*26*26+十位*26+个位
{
    return x * 26 * 26 + y * 26 + z;
}
 
string str(int x, int y, int z)  //转换字母函数
{
    string s;
    s += 'A' + x - 1;  //转换为字符形式
    s += 'A' + y - 1;
    s += 'A' + z - 1;
    return s;
}
 
int main() {
    int n = 2022;
    //2022一定是小于26*26*26,大于26*26,一定在AAA-ZZZ之间,因此循环三位
    for (int i = 1; i <= 26; i++)  //第三位,百位  AAA-ZZZ
        for (int j = 1; j <= 26; j++)  //第二位,十位 AA-ZZ
            for (int k = 1; k <= 26; k++)  // 个位  A-Z
                if (get(i, j, k) == n) //判断是否满足条件
                {
                    cout << str(i, j, k); //满足条件,则将该数转换成字母
                    return 0;
                }
 
    return 0;
}

3、日期相等

对于一个日期,我们可以计算出年份的各个数位上的数字之和,也可以分别计算月和日的各位数字之和。请问从 1900 年 1 月 1 日至 9999 年 12 月 31 日,总共有多少天,年份的数位数字之和等于月的数位数字之和加日的数位数字之和。例如,2022年11月13日满足要求,因为 2+0+2+2=(1+1)+(1+3) 。请提交满足条件的日期的总数量。

答案:70910


转换法

:日期转换为8位数的数字,再去枚举19000101到99991213之间的每个数字。我们需要先判断该数字是否为合法日期,再去判断年份的数位数字之和是否等于月的数位数字之和加日的数位数字之和

#include<iostream>
#include<algorithm>
#include<cstring>
 
using namespace std;
int res;
 
int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; //1,3,5,8,10,12月为31天,4,6,9,11为30天。
bool check_vaild(int year,int month,int day)  //判断年月日是否是正确的年月日,排除输入错误的年月日
{
    if (month == 0 || month > 12)
        return false;
 
    if (day == 0)
        return false;
    if (month != 2)
    {
        if (day > days[month])
            return false;
    }
    else
    {
        int leap = year % 400 == 0 || year % 4 == 0 && year % 100;
 
        if (day > days[month] + leap)  //闰年,2月+1=29
            return false;
    }
 
    return true;
}
 
 
int main()
{
    for (int i = 19000101; i <= 99991213; i++)
    {
        int year = i / 10000;
        int month = i % 10000 / 100;
        int day = i % 100;
 
        if (check_vaild(year, month, day))  //如果年月日正确,则判断是否满足题意
        {
            int x=0, y=0, z=0;
            for (int i = 0; i < 4; i++)
            {
                x += year % 10;
                year /= 10;
            }
 
            while (month)
            {
                y += month % 10;
                month /= 10;
            }
 
            while(day)
            {
                z += day % 10;
                day /= 10;
            }
 
            if (x == y + z)
                res++;
        }
    }
    cout<<res;
    return 0;
}

自己编写的暴力代码:

答案为70823.有知道哪里错了的大佬评论区帮我解释一下

#include<stdio.h>
// 1月3月5月7月8月10月12月都是31天,4月6月9月11月都是30天,平年2月28天,润年2月29天
int main(){
    int a,b,c,d,e,f,g,h,sum,sum1=0,sum2=0,sum3=0,sum4=0;
    for(int i=1900;i<=9999;i++){
        for(int j=1;j<=12;j++){
            if(j==1||j==3||j==5||j==7||j==8||j==10||j==12){
                for(int k=1;k<=31;k++){
                    a=i/1000;
                    b=i%1000/100;
                    c=i%1000%100/10;
                    d=i%1000%100%10;
                    e=j%10;
                    f=j/10;
                    g=k%10;
                    h=k/10;
                    if((a+b+c+d)==(e+f+g+h)){
                        sum1++;
                    }
                }
            }
            if(j==4||j==6||j==9||j==11){
                for(int k=1;k<=30;k++){
                    a=i/1000; //q千位
                    b=i%1000/100; //百位
                    c=i%1000%100/10;  //十位
                    d=i%1000%100%10; //个位
                    e=j%10;
                    f=j/10;
                    g=k%10;
                    h=k/10;
                    if((a+b+c+d)==(e+f+g+h)){
                        sum2++;
                    }
                }
            }
            if(j==2){

                if((i/400==0)||(i/4==0&&i/100!=0)){
                    for(int k=1;k<=29;k++){
                        a=i/1000;
                        b=i%1000/100;
                        c=i%1000%100/10;
                        d=i%1000%100%10;
                        e=j%10;
                        f=j/10;
                        g=k%10;
                        h=k/10;
                        if((a+b+c+d)==(e+f+g+h)){
                            sum3++;
                        }
                    }
                    
                }else{
                    for(int k=1;k<=28;k++){
                        a=i/1000;
                        b=i%1000/100;
                        c=i%1000%100/10;
                        d=i%1000%100%10;
                        e=j%10;
                        f=j/10;
                        g=k%10;
                        h=k/10;
                        if((a+b+c+d)==(e+f+g+h)){
                            sum4++;
                        }
                    }

                }
                

            }
            
        }
    }
    sum=sum1+sum2+sum3+sum4;
    printf("%d",sum);
    return 0;
}

4、

乘积方案数

小蓝有 30 个数,分别为:99, 22, 51, 63, 72, 61, 20, 88, 40, 21, 63, 30, 11, 18, 99, 12, 93, 16, 7, 53, 64, 9, 28, 84, 34, 96, 52, 82, 51, 77 。小蓝可以在这些数中取出两个序号不同的数,共有 30*29/2=435 种取法。请问这 435 种取法中,有多少种取法取出的两个数的乘积大于等于 2022 。

#include <iostream>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
int nums[31] = { 0, 99, 22, 51, 63, 72, 61, 20, 88, 40,
                21, 63, 30, 11, 18, 99, 12, 93, 16, 7, 53,
                64, 9, 28, 84, 34, 96, 52, 82, 51, 77 };
 
int main() {
 
    int res = 0;
 
    for (int i = 1; i <= 30; i++)
        for (int j = i + 1; j <= 30; j++) {
            if (nums[i] * nums[j] >= 2022) {
                res++;
            }
        }
 
    cout << res;
    return 0;
}

5、最大连通块

小蓝有一个 30 行 60 列的数字矩阵,矩阵中的每个数都是 0 或 1 。如果从一个标为 1 的位置可以通过上下左右走到另一个标为 1 的位置,则称两个位置连通。与某一个标为 1 的位置连通的所有位置(包括自己)组成一个连通分块。请问矩阵中最大的连通分块有多大?

#include <iostream>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
const int N = 35, M = 65;
 
int n = 30, m = 60;
char arr[N][M];
 
int dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };
 
int dfs(int x, int y) //判断每个遍历的元素是否满足条件
{
    int cnt = 1;
    arr[x][y] = '0';
 
    for (int i = 0; i < 4; i++) 
    {
        int a = x + dx[i], b = y + dy[i];  //判断周围4个数是否为1,
        if (a <= n && a >= 1 && b <= m && b >= 1 && arr[a][b] == '1') //第一个a,b必须是第二行第二列
        {
            cnt += dfs(a, b);//递归,满足条件是cnt+1且继续递归。
        }
    }
 
    return cnt;
}
 
int main() 
{
 
    int res = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> arr[i][j];  //输入数字矩阵
 
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (arr[i][j] == '1')  //当遍历到1时,判断是否满足位置连通
                res = max(res, dfs(i, j)); 
 
    cout << res << "\n";
    return 0;
 
}

6、

求星期几

给定一天是一周中的哪天,请问 n 天后是一周中的哪天?输入第一行包含一个整数 w,表示给定的天是一周中的哪天,w 为 1 到 6 分别表示周一到周六,w 为 7 表示周日。第二行包含一个整数 n。输出一行包含一个整数,表示 n 天后是一周中的哪天,1 到 6 分别表示周一到周六,7 表示周日。对于所有评测用例,1 <= n <= 1000000。


满足条件:自己凑

#include <iostream>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
int main()
{
    int w, n;
    cin >> w >> n;
 
    if ((w + n) % 7 == 0)
        cout << '7' << endl;
    else
        cout << (w + n) % 7 << endl;
 
    return 0;
}

#include<stdio.h>
int main(){
    int w=6,res;
    long int n=8;
    scanf("%d",&w);
    scanf("%d",&n);
    res=(n-(7-w))%7;
    if(res==0){
        res=7;
    }
    printf("%d",res);
    return 0;
}

7、

范围覆盖点数

题目描述:小蓝负责一块区域的信号塔安装,整块区域是一个长方形区域,建立坐标轴后,西南角坐标为 (0, 0), 东南角坐标为 (W, 0), 西北角坐标为 (0, H), 东北角坐标为 (W, H)。其中 W, H 都是整数。

他在 n 个位置设置了信号塔,每个信号塔可以覆盖以自己为圆心,半径为 R 的圆形(包括边缘)。

为了对信号覆盖的情况进行检查,小蓝打算在区域内的所有横纵坐标为整数的点进行测试,检查信号状态。其中横坐标范围为 0 到 W,纵坐标范围为 0 到 H,总共测试 (W+1) * (H+1) 个点。

给定信号塔的位置,请问这 (W+1)*(H+1) 个点中有多少个点被信号覆盖。

输入第一行包含四个整数 W, H, n, R,相邻整数之间使用一个空格分隔。

接下来 n 行,每行包含两个整数 x, y,表示一个信号塔的坐标。信号塔可能重合,表示两个信号发射器装在了同一个位置。

输出一行包含一个整数,表示答案。

对于所有评测用例,1 <= W, H <= 100,1 <= n <= 100, 1 <= R <= 100, 0 <= x <= W, 0 <= y <= H。

样例输入

10 10 2 5

0 0

7 0

样例输出

57


使用VScode运行上述样例时,运行很慢,很久才出结果,有人知道这是正常的还是哪里有问题

#include <iostream>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
typedef pair<int, int> PII;//pair是将2个数据组合成一组数据的一种数据类型,pair是通过struct结构体实现的
#define x first
#define y second
 
const int N = 110;
 
int w, h, n, R;
PII a[N];
 
int main() 
{
    cin >> w >> h >> n >> R; //横纵坐标,信号塔个数,信号塔覆盖范围
 
    for (int i = 0; i < n; i++)  //遍历每个信号塔,输入每个信号塔的坐标
        cin >> a[i].x >> a[i].y;  //信号塔的坐标
 
    int res = 0;
    for (int i = 0; i <= w; i++)
        for (int j = 0; j <= h; j++)  //暴力搜索范围内所有的点
        {
            bool flag = false;
            while(n--) 
            {
                int k = 0; 
                if ((i - a[k++].x) * (i - a[k++].x) +(j - a[k++].y) * (j - a[k++].y) <= R * R) 
                {
                    flag = true;
                    break;  //当flag为true时,直接结束。也就是该点至少在一个信函探测器里面。
                }
            }
 
            if (flag) res++;
        }
 
    cout << res;
    return 0;
}

8、

清理水草

小蓝有一个 n * m 大小的矩形水域,小蓝将这个水域划分为 n 行 m 列,行数从 1 到 n 标号,列数从 1 到 m 标号。每行和每列的宽度都是单位 1 。现在,这个水域长满了水草,小蓝要清理水草。每次,小蓝可以清理一块矩形的区域,从第 r1 行(含)到第 r2 行(含)的第 c1 列(含)到 c2 列(含)。经过一段时间清理后,请问还有多少地方没有被清理过。

输入第一行包含两个整数 n, m,用一个空格分隔。

第二行包含一个整数 t ,表示清理的次数。

接下来 t 行,每行四个整数 r1, c1, r2, c2,相邻整数之间用一个空格分隔,表示一次清理。请注意输入的顺序。

输出一行包含一个整数,表示没有被清理过的面积。

对于所有评测用例,1 <= r1 <= r2 <= n <= 100, 1 <= c1 <= c2 <= m <= 100, 0 <= t <= 100。

#include<iostream>
#include<algorithm>
#include<cstring>
 
using namespace std;
 
int r1, c1, r2, c2;
int n, m, t;
 
const int N = 110;
 
int a[N][N];
 
int main()
{
    cin >> n >> m;
 
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            a[i][j] = 1;  //1表示未清理过
    cin >> t;
    while (t--)
    {
        cin >> r1 >> c1 >> r2 >> c2;
        for (int i = r1; i <= r2; i++)
            for (int j = c1; j <= c2; j++)
                a[i][j] = 0;  //0表示已经被清理过
 
    }
 
    int res = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            res += a[i][j]; //所有的加起来,1的个数就是没有被清理的
 
    cout << res;
    return 0;
}

9、

滑行距离

小蓝准备在一个空旷的场地里面滑行,这个场地的高度不一,小蓝用一个 n 行 m 列的矩阵来表示场地,矩阵中的数值表示场地的高度。

如果小蓝在某个位置,而他上、下、左、右中有一个位置的高度(严格)低于当前的高度,小蓝就可以滑过去,滑动距离为 1 。

如果小蓝在某个位置,而他上、下、左、右中所有位置的高度都大于等于当前的高度,小蓝的滑行就结束了。

小蓝不能滑出矩阵所表示的场地。

小蓝可以任意选择一个位置开始滑行,请问小蓝最多能滑行多远距离。

输入第一行包含两个整数 n, m,用一个空格分隔。

接下来 n 行,每行包含 m 个整数,相邻整数之间用一个空格分隔,依次表示每个位置的高度。

输出一行包含一个整数,表示答案。

对于 30% 评测用例,1 <= n <= 20,1 <= m <= 20,0 <= 高度 <= 100。

对于所有评测用例,1 <= n <= 100,1 <= m <= 100,0 <= 高度 <= 10000。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
 
using namespace std;
 
const int N = 110;
 
int n, m;
int g[N][N];
bool f[N][N];
 
int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };
 
int dfs(int x, int y) 
{
    int res = 0;
    for (int i = 0; i < 4; i++) 
    {
        int a = x + dx[i], b = y + dy[i];
        if (a >= 1 && a <= n && b >= 1 && b <= m && !f[a][b] && g[x][y] > g[a][b]) 
        {
            f[a][b] = true;
            res = max(res, dfs(a, b) + 1);
 
            //还原现场
            f[a][b] = false;
        }
    }
 
    return res;
}
 
int main() 
{
    scanf("%d%d", &n, &m);
 
    int res = 0;
 
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            scanf("%d", &g[i][j]);
 
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) 
        {
            f[i][j] = true;  //表示当前位置已经走过
            res = max(res, dfs(i, j));
 
            //还原现场
            f[i][j] = false;
        }
 
    cout << res + 1 << "\n";
    return 0;
}

10、

序号最小值

小蓝有一个序列 a[1], a[2], …, a[n]。给定一个正整数 k,请问对于每一个 1 到 n 之间的序号 i,a[i-k], a[i-k+1], …, a[i+k] 这 2k+1 个数中的最小值是多少?当某个下标超过 1 到 n 的范围时,数不存在,求最小值时只取存在的那些值。

输入的第一行包含一整数 n。

第二行包含 n 个整数,分别表示 a[1], a[2], …, a[n]。

第三行包含一个整数 k 。

输出一行,包含 n 个整数,分别表示对于每个序号求得的最小值。

对于 30% 的评测用例,1 <= n <= 1000,1 <= a[i] <= 1000。

对于 50% 的评测用例,1 <= n <= 10000,1 <= a[i] <= 10000。

对于所有评测用例,1 <= n <= 1000000,1 <= a[i] <= 1000000。

#include <iostream>
#include <algorithm>
#include <cmath>
 
using namespace std;
 
const int N = 1e6 + 10, M = 20;
 
int n, k, t;
int q[N];
int f[N][M]; 
 
int query(int l, int r) 
{
    int len = log(r - l + 1) / log(2); 
    int x = f[l][len], y = f[r - (1 << len) + 1][len]; 
    return q[x] > q[y] ? y : x;
}int main() 
{
    cin >> n; 
    for (int i = 1; i <= n; i++) 
        scanf("%d", &q[i]); 
 
    cin >> k;
 
    t = log(n) / log(2); 
    for (int j = 0; j <= t; j++) 
    { 
        for (int i = 1; i + (1 << j) - 1 <= n; i++) 
        { 
            if (!j) 
                f[i][j] = i; 
            else 
            { 
                int l = f[i][j - 1], r = f[i + (1 << (j - 1))][j - 1]; 
                if (q[l] > q[r]) 
                    f[i][j] = r; 
                else 
                    f[i][j] = l; 
            } 
        } 
    }
    int l, r; 
    for (int i = 1; i <= n; i++) 
    { 
        l = max(1, i - k), r = min(n, i + k); 
        cout << q[query(l, r)] << " ";
    }
    cout << endl;
    return 0;
}



版权声明:本文为qq_59572329原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。