蓝桥杯刷题 -- 第六届蓝桥杯

题头:本内容所有题面都来自博客:https://blog.csdn.net/ryo_218/article/details/79704030在此感谢!

1,奖券数目

有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利。
虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求。某抽奖活动的奖券号码是5位数(10000-99999),要求其中不要出现带“4”的号码,主办单位请你计算一下,如果任何两张奖券不重号,最多可发出奖券多少张。

思路:5重循环,搞定。

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int ans = 0;
    for(int a = 1; a <= 9; ++a)
        for(int b = 0; b <= 9; ++b)
            for(int c = 0; c <= 9; ++c)
                for(int d = 0; d <= 9; ++d)
                    for(int e = 0; e <= 9; ++e)
                    {
                        if(a != 4 && b != 4 && c != 4 && d != 4 && e != 4)
                        {
                            ans++;
                        }
                    }
    printf("%d
", ans);
}
View Code

2、

星系炸弹
在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。
每个炸弹都可以设定多少天之后爆炸。
比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。
有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。
请填写该日期,格式为 yyyy-mm-dd  即4位年份2位月份2位日期。比如:2015-02-19
请严格按照格式书写。不能出现其它文字或符号。

思路:

1.EXCEL表格做:https://blog.csdn.net/whd526/article/details/50719483

2.普通思路:

#include <iostream>
#include <cstdio>

using namespace std;
int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
    int dd = 9, mm = 11, yy = 2014;
    int n = 1000;
    int cnt = 0;
    for(int i = mm-1; i < 12; ++i)
    {
        cnt += day[i];
    }
    cnt -= dd+1;
    //printf("%d
", cnt);
    int flag = 0;
    for(int i = yy+1; i <= 2020; ++i)
    {
        for(int j = 0; j < 12; ++j)
        {
            if((yy%4==0&&yy%100!=0)||(yy%400 != 0))
            {
                if(j==1) cnt += (day[j]+1);
                else cnt += day[j];
            }
            else cnt += day[j];
            if(cnt >= 1000)
            {
                if((yy%4==0&&yy%100!=0)||(yy%400 != 0))
                {
                    if(j==1) cnt -= (day[j]+1);
                    else cnt -= day[j];
                }
                else cnt -= day[j];
                mm = j+1;
                dd = n-cnt+1;
                yy = i;
                flag = 1;
                break;
            }
        }
        if(flag) break;
    }

    printf("%d-%d-%d", yy, mm, dd);
}
View Code

要注意的是,开始和结束的天也算进去。

3、

三羊献瑞
观察下面的加法算式:
        祥 瑞 生 辉
  +   三 羊 献 瑞
-------------------
   三 羊 生 瑞 气
(如果有对齐问题,可以参看【图1.jpg】)

其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。
请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。

思路:全排列一下就行。

 代码:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>

using namespace std;

int main()
{
    int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    while(next_permutation(a, a+10))
    {
        if(a[4] != 0 && a[0] != 0)
        {
            int x1 = a[0]*1000+a[1]*100+a[2]*10+a[3];
            int x2 = a[4]*1000+a[5]*100+a[6]*10+a[1];
            int x3 = a[4]*10000+a[5]*1000+a[2]*100+a[1]*10+a[7];
            if(x1+x2 == x3)
            {
                printf("%d
", x2);
                break;
            }
        }

    }

}
View Code

---恢复内容结束---

题头:本内容所有题面都来自博客:https://blog.csdn.net/ryo_218/article/details/79704030在此感谢!

1,奖券数目

有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利。
虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求。某抽奖活动的奖券号码是5位数(10000-99999),要求其中不要出现带“4”的号码,主办单位请你计算一下,如果任何两张奖券不重号,最多可发出奖券多少张。

思路:5重循环,搞定。

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int ans = 0;
    for(int a = 1; a <= 9; ++a)
        for(int b = 0; b <= 9; ++b)
            for(int c = 0; c <= 9; ++c)
                for(int d = 0; d <= 9; ++d)
                    for(int e = 0; e <= 9; ++e)
                    {
                        if(a != 4 && b != 4 && c != 4 && d != 4 && e != 4)
                        {
                            ans++;
                        }
                    }
    printf("%d
", ans);
}
View Code

2、

星系炸弹
在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。
每个炸弹都可以设定多少天之后爆炸。
比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。
有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。
请填写该日期,格式为 yyyy-mm-dd  即4位年份2位月份2位日期。比如:2015-02-19
请严格按照格式书写。不能出现其它文字或符号。

思路:

1.EXCEL表格做:https://blog.csdn.net/whd526/article/details/50719483

2.普通思路:

#include <iostream>
#include <cstdio>

using namespace std;
int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
    int dd = 9, mm = 11, yy = 2014;
    int n = 1000;
    int cnt = 0;
    for(int i = mm-1; i < 12; ++i)
    {
        cnt += day[i];
    }
    cnt -= dd+1;
    //printf("%d
", cnt);
    int flag = 0;
    for(int i = yy+1; i <= 2020; ++i)
    {
        for(int j = 0; j < 12; ++j)
        {
            if((yy%4==0&&yy%100!=0)||(yy%400 != 0))
            {
                if(j==1) cnt += (day[j]+1);
                else cnt += day[j];
            }
            else cnt += day[j];
            if(cnt >= 1000)
            {
                if((yy%4==0&&yy%100!=0)||(yy%400 != 0))
                {
                    if(j==1) cnt -= (day[j]+1);
                    else cnt -= day[j];
                }
                else cnt -= day[j];
                mm = j+1;
                dd = n-cnt+1;
                yy = i;
                flag = 1;
                break;
            }
        }
        if(flag) break;
    }

    printf("%d-%d-%d", yy, mm, dd);
}
View Code

要注意的是,开始和结束的天也算进去。

3、

三羊献瑞
观察下面的加法算式:
        祥 瑞 生 辉
  +   三 羊 献 瑞
-------------------
   三 羊 生 瑞 气
(如果有对齐问题,可以参看【图1.jpg】)

其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。
请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。

思路:全排列一下就行。

 代码:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>

using namespace std;

int main()
{
    int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    while(next_permutation(a, a+10))
    {
        if(a[4] != 0 && a[0] != 0)
        {
            int x1 = a[0]*1000+a[1]*100+a[2]*10+a[3];
            int x2 = a[4]*1000+a[5]*100+a[6]*10+a[1];
            int x3 = a[4]*10000+a[5]*1000+a[2]*100+a[1]*10+a[7];
            if(x1+x2 == x3)
            {
                printf("%d
", x2);
                break;
            }
        }

    }

}
View Code

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("+
");
    
    for(k=1; k<(height-1)/2;k++){
        printf("|");
        for(i=0;i<width-2;i++) printf(" ");
        printf("|
");
    }
    
    printf("|");
    
    printf("%*s%s%*s",_____________________________________________);  //填空
              
    printf("|
");
    
    for(k=(height-1)/2+1; k<height-1; k++){
        printf("|");
        for(i=0;i<width-2;i++) printf(" ");
        printf("|
");
    }    
    
    printf("+");
    for(i=0;i<width-2;i++) printf("-");
    printf("+
");    
}
 
int main()
{
    StringInGrid(20,6,"abcd1234");
    return 0;
}

思路:

这道题主要是要理解函数printf("%*s", width, string);这个函数项当于printf("%xs", string), 其中x是就是width,未知数,动态输出。

所以,要使其放置中间,可以知道*填的就是空白的长度。

答案就是:width-strlen(buf)-2)/2," ",buf,(width-strlen(buf)-1)/2," "

5、

加法变乘法
我们都知道:1+2+3+ ... + 49 = 1225
现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015
比如:
1+2+3+...+10*11+12+...+27*28+29+...+49 = 2015
就是符合要求的答案。
请你寻找另外一个可能的答案,并把位置靠前的那个乘号左边的数字提交(对于示例,就是提交10)。
注意:需要你提交的是一个整数,不要填写任何多余的内容。

思路,枚举一下就行:

#include <iostream>
#include <cstdio>

using namespace std;
const int MX = 100+10;
int a[MX];
int main()
{
    int cnt = 0;
    //for(int i = 1; i <= 49; ++i) a[i] = i;
    for(int i = 1; i <= 49; ++i)
    {
        int x = i*(i+1);
        for(int j = i+2; j <= 49; ++j)
        {
            int y = j*(j+1);
            if((1225-(2*i+1)-(2*j+1)+x+y) == 2015)
                printf("%d %d | %d %d
", i, i+1, j, j+1);
        }

    }
}
View Code

6、

牌型种数
小明被劫持到X赌城,被迫与其他3人玩牌。
一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张。
这时,小明脑子里突然冒出一个问题:
如果不考虑花色,只考虑点数,也不考虑自己得到的牌的先后顺序,自己手里能拿到的初始牌型组合一共有多少种呢?
请填写该整数,不要填写任何多余的内容或说明文字。

这个又2个思路:

1、暴力枚举:

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
const int MX = 100+10;
int a[MX];

int main()
{
    int cnt = 0;
    for(int n1 = 0; n1 <= 4; ++n1)
        for(int n2 = 0; n2 <= 4; ++n2)
            for(int n3 = 0; n3 <= 4; ++n3)
                for(int n4 = 0; n4 <= 4; ++n4)
                    for(int n5 = 0; n5 <= 4; ++n5)
                        for(int n6 = 0; n6 <= 4; ++n6)
                            for(int n7 = 0; n7 <= 4; ++n7)
                                for(int n8 = 0; n8 <= 4; ++n8)
                                    for(int n9 = 0; n9 <= 4; ++n9)
                                        for(int n10 = 0; n10 <= 4; ++n10)
                                            for(int n11 = 0; n11 <= 4; ++n11)
                                                for(int n12 = 0; n12 <= 4; ++n12)
                                                    for(int n13 = 0; n13 <= 4; ++n13)
                                                        if(n1+n2+n3+n4+n5+n6+n7+n8+n9+n10+n11+n12+n13 == 13) cnt++;
   printf("%d", cnt);

}
View Code

2、DFS

#include <iostream>
#include <cstdio>

using namespace std;
int ans = 0;

void dfs(int cnt, int kind)
{
    if(cnt > 13 || kind > 13) return;
    if(cnt == 13 && kind <= 13)
    {
        ans++;
        return;
    }
    for(int i = 0; i <= 4; ++i)
    {
        dfs(cnt+i, kind+1);
    }
}

int main()
{
    dfs(0, 0);
    printf("%d", ans);
}
View Code

7、

移动距离
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到10000范围内
w为排号宽度,m,n为待计算的楼号。
要求输出一个整数,表示m n 两楼间最短移动距离。

例如:
用户输入:
6 8 2
则,程序应该输出:
4

再例如:
用户输入:
4 7 20
则,程序应该输出:
5

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

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

思路:观察一下规律,可以发现,2个坐标差的绝对值的和就是答案。

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int get_pos(int &x, int &y, int m, int w)
{
    x = m%w==0?(m/w-1):(m/w);
    if(x%2) y = w-(m-w*x);
    else y = m-w*x-1;
    return 0;
}

int main()
{
    int w, m, n;
    scanf("%d%d%d", &w, &m, &n);
    int x1, x2, y1, y2;
    get_pos(x1, y1, m, w);
    get_pos(x2, y2, n, w);
    int ans = abs(x1-x2)+abs(y1-y2);
    printf("%d", ans);

}
View Code
化繁为简 大巧不工
原文地址:https://www.cnblogs.com/mpeter/p/10428516.html