第六届蓝桥杯真题总结

第一题:奖券数目

有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利。

虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求。某抽奖活动的奖券号码是5位数(10000-99999),要求其中不要出现带“4”的号码,
主办单位请你计算一下,如果任何两张奖券不重号,
最多可发出奖券多少张。 请提交该数字(一个整数),不要写任何多余的内容或说明性文字。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

void solve()
{
    int ans = 0;
    for (int a = 1; a <= 9; a++)
    {
        if (a == 4) continue;
        for (int b = 0; b <= 9; b++)
        {
            if (b == 4) continue;
            for (int c = 0; c <= 9; c++)
            {
                if (c == 4) continue;
                for (int d = 0; d <= 9; d++)
                {
                    if (d == 4) continue;
                    for (int e = 0; e <= 9; e++)
                    {
                        if (e == 4) continue;
                        ans++;        
                    }    
                }    
            } 
        }
    }
    cout << ans << endl;
}

int main()
{
    solve();
    
    return 0;
}

运行结果是:52488.

第二题 星系炸弹

在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。

每个炸弹都可以设定多少天之后爆炸。

比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。

有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。

请填写该日期,格式为 yyyy-mm-dd 即4位年份2位月份2位日期。比如:2015-02-19

请严格按照格式书写。不能出现其它文字或符号。

使用Excel:

代码解法:

#include <iostream>
using namespace std;

int year = 1777, month = 4, day = 30;

bool IsEndofMonth();
void AddDay(int days);
void IncDay();
bool IsLeapYear();

bool IsLeapYear()
{
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 
}

bool IsEndofMonth()
{
    switch(month)
    {
        case 4:
        case 6:
        case 9:
        case 11: return day == 30;
        case 2:
            if (IsLeapYear())
                return day == 29;
            else
                return day == 28;
        default:
            return day == 31;
    }
}

void IncDay()             //增加一天
{
    if(IsEndofMonth())         //增加天数,记得要判断是否是年末,月末 
    {
        if(month == 12)
        {
            day = 1; month = 1; year++;
        }
        else {                  //已经是IsEndMonth 
            day = 1; month++;
        }
    } 
    else {
        day++; 
    }
}

void AddDay(int days)
{
    for (int i = 1; i <= days; i++)    //增加多少天 days - 1
    {
        IncDay(); 
    }
}

int main()
{
//    AddDay(5343);
    cin >> year >> month >> day;
    AddDay(1000);
    cout << year << "-" << month << "-" << day << endl;
    
    return 0;
} 

第三题

观察下面的加法算式:
祥 瑞 生 辉
+ 三 羊 献 瑞 ------------------- 三 羊 生 瑞 气
其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。 请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。
#include <iostream>
#include <algorithm>
using namespace std;

void solve()
{
    int num[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    do {
        int a = num[0]*1000 + num[1]*100 + num[2]*10 + num[3];
        int b = num[4]*1000 + num[5]*100 + num[6]*10 + num[1];
        int c = num[4]*10000 + num[5]*1000 + num[2]*100 + num[1]*10 + num[7];
        if (a + b == c && b > 1000) {
            cout << b << endl;
        }
    } while (next_permutation(num, num+10));
}

int main()
{
    solve();
    
    return 0;
}

第四题 格子中输出

StringInGrid函数会在一个指定大小的格子中打印指定的字符串。

要求字符串在水平、垂直两个方向上都居中。

如果字符串太长,就截断。

如果不能恰好居中,可以稍稍偏左或者偏上一点。

下面的程序实现这个逻辑,请填写划线部分缺少的代码。

#include <stdio.h>

#include <string.h>

void StringInGrid(intwidth, 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; } 对于题目中数据,应该输出: +------------------+ | | | abcd1234 | | | | | +------------------+
(如果出现对齐问题,参看【图1.jpg】

注解:printf("%*s", len, "xxx"):相当于是 (len, "str"),  设置输出字符 为len位,  如果str长度不足len位, 前面补空格 

printf("%*s%s%*s", (width-2-strlen(s))/2, "", s, (width-2-strlen(s))/2, "");  //填空

第五题:九数组分数

1,2,3...9 这九个数字组成一个分数,其值恰好为1/3,如何组法?
下面的程序实现了该功能,请填写划线部分缺失的代码。
#include
<stdio.h> void test(int x[]) { int a = x[0]*1000 + x[1]*100 + x[2]*10 +x[3]; int b = x[4]*10000 + x[5]*1000 + x[6]*100 +x[7]*10 + x[8]; if(a*3==b) printf("%d / %d ", a,b); } void f(int x[],int k) { int i,t; if(k>=9){ test(x); return; } for(i=k; i<9; i++){ {t=x[k]; x[k]=x[i]; x[i]=t;} f(x,k+1); _____________________________________________// 填空处 } }
int main() { int x[] = {1,2,3,4,5,6,7,8,9}; f(x,0); return 0; }

题解:一个简单的回溯问题

{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)。 注意:需要你提交的是一个整数,不要填写任何多余的内容。
#include <iostream>
using namespace std;


void solve()
{
    int i, j;
    for (i = 1; i < 47; i++)
    {
        for (j = i + 2; j <= 49; j++)
        {
            int sum = 0;
            sum += i*(i+1);
            sum += j*(j+1);
            sum = (1+49)/2*49 + sum - ((i+i+1+j+j+1));
            if (sum == 2015) {
                cout << i << endl;
                
            }
        }
    }

}

int main()
{
    solve();
    
    return 0;    
} 

运行结果是:16

第七题:牌型种数

小明被劫持到X赌城,被迫与其他3人玩牌。

一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张。

这时,小明脑子里突然冒出一个问题:

如果不考虑花色,只考虑点数,也不考虑自己得到的牌的先后顺序,自己手里能拿到的初始牌型组合一共有多少种呢?

请填写该整数,不要填写任何多余的内容或说明文字。

题解:回溯问题...每种牌型可能拿到 0,1,2,3,4 张的可能,就是说,回溯搜索13种牌型,分别是0,1,2,3,4的可能....递归结束条件:牌型遍历完,且当前拿牌的张数为13;

#include <iostream>
using namespace std;

typedef long long LL;
const int maxn = 10000000;
bool used[maxn];

int ans;
int cur_sum;

void dfs(int step)
{
    //当前手上牌的数目 
    if (cur_sum > 13) {
        return;
    }
    //13种牌型遍历完 
    if (step == 14) 
    {
        if (cur_sum == 13) {
            ans++;
        }
        return;
    }
    
    // 0, 1, 2, 3, 4 每种牌可能拥有的张数 
    for (int i = 0; i < 5; i++)
    {
        cur_sum += i;
        dfs(step + 1);
        cur_sum -= i;  //回溯 
    }
}

void solve()
{
    //从第一种牌型搜索 
    dfs(1);
    cout << ans << endl;
}

int main()
{
    solve();
    
    return 0;
}

第八题:移动距离

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
原文地址:https://www.cnblogs.com/douzujun/p/8569467.html