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

1.标题:啤酒和饮料  

    啤酒每罐2.3元,饮料每罐1.9元。小明买了若干啤酒和饮料,一共花了82.3元。 
    我们还知道他买的啤酒比饮料的数量少,请你计算他买了几罐啤酒。     注意:答案是一个整数。请通过浏览器提交答案。 

不要书写任何多余的内容(例如:写了饮料的数量,添加说明文字等)。

答案:11(好像double不能进行等于比较,不然会没输出,最好扩大10倍)

#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 main()
{
    for(int i=1; i<=40; i++)
    {
        for(int j=i+1; j<=50; j++)
        {
            if(23*i+19*j == 823)
            {
                printf("%d\n",i);
                break;
            }
        }
    }
    return 0;
}

2.标题:切面条 

    一根高筋拉面,中间切一刀,可以得到2根面条。     如果先对折1次,中间切一刀,可以得到3根面条。     如果连续对折2次,中间切一刀,可以得到5根面条。     那么,连续对折10次,中间切一刀,会得到多少面条呢? 答案是个整数,请通过浏览器提交答案。不要填写任何多余的内容。

大神的博客:http://www.bubuko.com/infodetail-733271.html(不会写)

答案:

3.标题:李白打酒 
    话说大诗人李白,一生好饮。幸好他从不开车。 
    一天,他提着酒壶,从家里出来,酒壶中有酒2斗。他边走边唱:     无事街上走,提壶去打酒。     逢店加一倍,遇花喝一斗。  
    这一路上,他一共遇到店5次,遇到花10次,已知最后一次遇到的是花,他正好把酒喝光了。  
    请你计算李白遇到店和花的次序,可以把遇店记为a,遇花记为b。则:babaabbabbabbbb 就是合理的次序。像这样的答案一共有多少呢?请你计算出所有可能方案的个数(包含题目给出的)。 
    注意:通过浏览器提交答案。答案是个整数。不要书写任何多余的内容。

答案:14(简单深搜)

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int sum;
void dfs(int store,int flower,int wine)
{
    if (store == 5 && flower == 10 && wine == 0) {
        sum++;
        return;
    }
    //这里0直接退出保证了最后酒为0时,遇到的一定是花,因为他不能拿着空酒瓶加倍
    if (wine <= 0 || store > 5 || flower > 10)
        return;
    dfs(store+1,flower,wine * 2);
    dfs(store,flower+1,wine - 1);
}
int main()
{
    sum = 0;
    dfs(0,0,2);
    //14
    cout << sum << endl;
}

#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 sum = 0;

void dfs(int drink,int store,int flower)
{
    if(store == 5 && flower == 10 && drink == 0)
    {
        sum++;
        return;
    }
    if(drink <= 0)
        return;
    if(store > 5)
        return;
    if(flower > 10)
        return;
    dfs(drink-1,store,flower+1);
    dfs(2*drink,store+1,flower);
}

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

4.

标题:史丰收速算

 

    史丰收速算法的革命性贡献是:从高位算起,预测进位。不需要九九表,彻底颠覆了传统手算!

 

    速算的核心基础是:1位数乘以多位数的乘法。

 

    其中,乘以7是最复杂的,就以它为例。

 

    因为,1/7是个循环小数:0.142857...,如果多位数超过142857...,就要进1

 

    同理,2/7,3/7, ... 6/7也都是类似的循环小数,多位数超过 n/7,就要进n

 

    下面的程序模拟了史丰收速算法中乘以7的运算过程。

 

    乘以 7 的个位规律是:偶数乘以2,奇数乘以2再加5,都只取个位。

 

    乘以 7 的进位规律是:

    142857...1,

    285714...2,

    428571...3,

    571428...4,

    714285...5,

    857142...6

 

    请分析程序流程,填写划线部分缺少的代码。

241876844562801

 

//计算个位

int ge_wei(int a)

{

    if(a % 2 ==0)

        return (a* 2) % 10;

    else

        return (a* 2 + 5) % 10;   

}

 

//计算进位

int jin_wei(char* p)

{

    char*level[] = {

        "142857",

        "285714",

        "428571",

        "571428",

        "714285",

        "857142"

    };

   

    char buf[7];

    buf[6] ='\0';

    strncpy(buf,p,6);

   

    int i;

    for(i=5;i>=0; i--){

        int r =strcmp(level[i], buf);

        if(r<0)return i+1;

        while(r==0){

            p +=6;

            strncpy(buf,p,6);

            r =strcmp(level[i], buf);

            if(r<0)return i+1;

            ______________________________;  //填空

        }

    }

   

    return 0;

}

 

//多位数乘以7

void f(char* s)

{

    int head =jin_wei(s);

    if(head >0) printf("%d", head);

   

    char* p = s;

    while(*p){

        int a =(*p-'0');

        int x =(ge_wei(a) + jin_wei(p+1)) % 10;

        printf("%d",x);

        p++;

    }

   

    printf("\n");

}

 

int main()

{

    f("428571428571");

    f("34553834937543");       

    return 0;

}

 

 

注意:通过浏览器提交答案。只填写缺少的内容,不要填写任何多余的内容(例如:说明性文字)

答案:if(r>0) return i(完全看不懂)

5.

标题:打印图形

 

    小明在X星球的城堡中发现了如下图形和文字:

rank=3

   *

  * *

 *   * 

* * * *

 

rank=5

              *                                                     

              **                                                    

            *   *                                                    

            * ** *                                                  

          *       *                                                 

          **     * *                                                

         *   *  *   *                                               

        * * * ** * * *                                              

       *               *                                             

      * *             * *                                             

     *   *          *   *                                           

    * * * *         * * * *                                          

   *       *      *       * 

  * *     * *    * *     * * 

 *   *  *   *   *   *   *   *

* * * * * * * * * * * * * * * * 

 

ran=6

                               *                                     

                              * *                                    

                             *   *                                   

                            * * * *                                  

                           *       *                                 

                          * *     * *                                

                         *   *  *   *                                

                       * * * * * * * *                              

                      *               *                             

                     * *             * *                            

                    *   *           *  *                           

                   * * * *         * * * *                          

                  *       *       *      *                         

                 * *     * *     * *    * *                        

                *   *   *  *   *   *  *   *                       

               * * * * * * * * * * * * * * * *                      

              *                              *                     

              **                             * *                    

            *   *                           *   *                   

            * ** *                         * * * *                  

          *       *                       *       *                 

          **     * *                     * *     * *                

         *   *  *   *                   *   *  *   *               

        * * * ** * * *                 * * * * * * **              

       *               *               *               *             

      * *             * *             * *             * *            

     *   *          *   *           *  *           *   *           

    * * * *         * * * *         * * * *         * * * *          

   *      *       *       *      *       *       *      *         

  * *     * *    * *     * *     * *    * *     * *     * *        

 *   *  *   *   *   *   *  *   *   *  *   *   *  *   *   *       

* * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * *      

                                                                     

 

    小明开动脑筋,编写了如下的程序,实现该图形的打印。

 

#define N 70

 

void f(char a[][N], int rank, int row, int col)

{

    if(rank==1){

        a[row][col]= '*';

        return;

    }

   

    int w = 1;

    int i;

    for(i=0;i<rank-1; i++) w *= 2;

   

    ____________________________________________;

    f(a, rank-1,row+w/2, col);

    f(a, rank-1,row+w/2, col+w);

}

 

int main()

{

    char a[N][N];

    int i,j;

    for(i=0;i<N;i++)

    for(j=0;j<N;j++)a[i][j] = ' ';

   

    f(a,6,0,0);

   

    for(i=0;i<N; i++){

        for(j=0;j<N; j++) printf("%c",a[i][j]);

        printf("\n");

    }

   

    return 0;

}

 

 

    请仔细分析程序逻辑,填写缺失代码部分。

 

    通过浏览器提交答案。注意不要填写题目中已有的代码。也不要写任何多余内容(比如说明性的文字)


答案: f(a,rank-1,row,col+w/2);

分析个大概意思,直接调代码,很快就会出来

6.


标题:奇怪的分式

 

    上小学的时候,小明经常自己发明新算法。一次,老师出的题目是:

 

    1/4 乘以8/5

 

    小明居然把分子拼接在一起,分母拼接在一起,答案是:18/45(参见图1.png

 

    老师刚想批评他,转念一想,这个答案凑巧也对啊,真是见鬼!

 

    对于分子、分母都是 1~9中的一位数的情况,还有哪些算式可以这样计算呢?

 

    请写出所有不同算式的个数(包括题中举例的)。

 

    显然,交换分子分母后,例如:4/1乘以 5/8是满足要求的,这算做不同的算式。

 

    但对于分子分母相同的情况,2/2乘以 3/3这样的类型太多了,不在计数之列!

 

注意:答案是个整数(考虑对称性,肯定是偶数)。请通过浏览器提交。不要书写多余的内容。


答案:14

#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;

struct node
{
    int up;
    int down;
};

int gcd(int a,int b)
{
    if(b == 0)
        return a;
    else
        return gcd(b,a%b);
}

int main()
{
    node a,b;
    int sum = 0;
    for(int i=1; i<=9; i++)
    {
        for(int j=1; j<=9; j++)
        {
            if(i == j)
                continue;
            for(int k=1; k<=9; k++)
            {
                for(int l=1; l<=9; l++)
                {
                    if(k == l)
                        continue;
                    a.up = i*k;
                    a.down = j*l;
                    int cf = gcd(a.up,a.down);
                    a.up /= cf;
                    a.down /= cf;
                    b.up = i*10+k;
                    b.down = j*10+l;
                    cf = gcd(b.up,b.down);
                    b.up /= cf;
                    b.down /= cf;
                    if(a.up == b.up && a.down == b.down)
                    {
                        sum++;
                    }
                }
            }
        }
    }
    printf("%d\n",sum);
    return 0;
}

7


标题:六角填数

 

    如图【1.png】所示六角形中,填入1~12的数字。

 

    使得每条直线上的数字之和都相同。

 

    图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?

 

请通过浏览器提交答案,不要填写多余的内容。


答案:10(回溯一下就行)

#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 a[20];
int visit[20] = {0};
int b[7];

void dfs(int cur)
{
	int i;
	if(cur == 11)
	{
		b[0] = a[0] + a[2] + a[5] + a[7];
		b[1] = a[0] + a[3] + a[6] + a[10];
		b[2] = a[7] + a[8] + a[9] + a[10];
		b[3] = a[1] + a[2] + a[3] + a[4];
		b[4] = a[1] + a[5] + a[8] + a[11];
		b[5] = a[4] + a[6] + a[9] + a[11];
		for(i=0; i<5; i++)
		{
			if(b[i] != b[i+1])
				break;	
		}
		if(i == 5)
		{
			printf("%d",a[5]);
		}
		return;
	}
	for(i=2; i<=12; i++)
	{
		if(!visit[i])
		{
			visit[i] = 1;
			a[cur] = i;
			dfs(cur+1);
			visit[i] = 0;
		}
	}
}

int main()
{
	a[0] = 1;
	a[1] = 8;
	a[11] = 3;
	visit[1] = 1;
	visit[3] = 1;
	visit[8] = 1;
	dfs(2);
	return 0;
}

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值