2012第三届蓝桥杯本科组 C/C++真题及题解

1. (3')微生物增殖

    假设有两种微生物 X 和 Y
    X出生后每隔3分钟分裂一次(数目加倍),Y出生后每隔2分钟分裂一次(数目加倍)。
    一个新出生的X,半分钟之后吃掉1个Y,并且,从此开始,每隔1分钟吃1个Y。
    现在已知有新出生的 X=10, Y=89,求60分钟后Y的数目。
    如果X=10,Y=90  呢?

    本题的要求就是写出这两种初始条件下,60分钟后Y的数目。
    题目的结果令你震惊吗?这不是简单的数字游戏!真实的生物圈有着同样脆弱的性质!也许因为你消灭的那只 Y 就是最终导致 Y 种群灭绝的最后一根稻草!

    请忍住悲伤,把答案写在“解答.txt”中,不要写在这里!

代码:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
#include<ctime>
using namespace std;
int main() {
	int x,y;
	cin>>x>>y;
	for(int i =1;i<=60;i++){
		y-=x;
		if(i%3 == 0) x*=2;
		if(i%2 == 0) y*=2;
	}
	y = (y<0)?0:y;
	cout<<y<<endl;
	return 0;
}
//输出:0 94371840


2. (4')古堡算式

    福尔摩斯到某古堡探险,看到门上写着一个奇怪的算式:
    ABCDE * ? = EDCBA
    他对华生说:“ABCDE应该代表不同的数字,问号也代表某个数字!”
    华生:“我猜也是!”
    于是,两人沉默了好久,还是没有算出合适的结果来。
    请你利用计算机的优势,找到破解的答案。
    把 ABCDE 所代表的数字写出来。
    答案写在“解答.txt”中,不要写在这里! 

分析:枚举

代码:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
#include<ctime>
using namespace std;
int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
int main() {
	do {
		int res = a[0] * 10000 + a[1] * 1000 + a[2] * 100 + a[3] * 10 + a[4];
		int x1 = a[4] * 10000 + a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
		int x2 = res / x1;
		if (a[0] == 0 || a[4] == 0||x2 == 0) continue;
		if (x1*x2 == res) {//整数 
			cout << x1 << "*" << x2 << "=" << res << endl;
			break;
		}
	} while (next_permutation(a,a+10));
	system("pause");
	return 0;
}
//输出:21978*4=87912



3. (5')比酒量

    有一群海盗(不多于20人),在船上比拼酒量。过程如下:打开一瓶酒,所有在场的人平分喝下,有几个人倒下了。再打开一瓶酒平分,又有倒下的,再次重复...... 直到开了第4瓶酒,坐着的已经所剩无几,海盗船长也在其中。当第4瓶酒平分喝下后,大家都倒下了。
    等船长醒来,发现海盗船搁浅了。他在航海日志中写到:“......昨天,我正好喝了一瓶.......奉劝大家,开船不喝酒,喝酒别开船......”
    请你根据这些信息,推断开始有多少人,每一轮喝下来还剩多少人。
    如果有多个可能的答案,请列出所有答案,每个答案占一行。
    格式是:人数,人数,...
    例如,有一种可能是:20,5,4,2,0
    答案写在“解答.txt”中,不要写在这里!

分析:枚举

代码:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
#include<ctime>
using namespace std;
int main() {
	double sum;
	for(int i =1;i<21;i++){
		for(int j = i+1;j<21;j++){
			for(int k =j+1;k<21;k++){
				for(int f = k+1;f<21;f++){
					sum = 1.0/i+1.0/j+1.0/k+1.0/f;
					if(fabs(sum-1)<1e-8)
					cout<<f<<" "<<k<<" "<<j<<" "<<i<<" "<<"0"<<endl;
				}
			}
		}
	}
	return 0;
}



4. (8‘)奇怪的比赛

    某电视台举办了低碳生活大奖赛。题目的计分规则相当奇怪:
    每位选手需要回答10个问题(其编号为1到10),越后面越有难度。答对的,当前分数翻倍;答错了则扣掉与题号相同的分数(选手必须回答问题,不回答按错误处理)。
    每位选手都有一个起步的分数为10分。
    某获胜选手最终得分刚好是100分,如果不让你看比赛过程,你能推断出他(她)哪个题目答对了,哪个题目答错了吗?
    如果把答对的记为1,答错的记为0,则10个题目的回答情况可以用仅含有1和0的串来表示。例如:0010110011 就是可能的情况。
    你的任务是算出所有可能情况。每个答案占一行。
    答案写在“解答.txt”中,不要写在这里!

分析:dfs

代码:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
#include<ctime>
using namespace std;
int state[11];
void dfs(int score,int step){//分别表示分数,答道第几题 
	if(step == 11){
		if(score == 100){
			for(int i =1;i<=10;i++) cout<<state[i];
			cout<<endl;
		}
		return;
	}
	//答对 
	state[step] = 1;
	dfs(score*2,step+1);
	state[step] = 0;
	dfs(score - step,step+1);
	state[step] = -1;
	return;
}
int main() {
	memset(state,-1,sizeof(state));
	dfs(10,1);
	return 0;
}
/*输出:
1011010000
0111010000
0010110011*/ 



5. (6')转方阵

    对一个方阵转置,就是把原来的行号变列号,原来的列号变行号
    例如,如下的方阵:
 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15 16

    转置后变为:
 1  5  9 13
 2  6 10 14
 3  7 11 15
 4  8 12 16

    但,如果是对该方阵顺时针旋转(不是转置),却是如下结果:
13  9  5  1
14 10  6  2
15 11  7  3
16 12  8  4

    下面的代码实现的功能就是要把一个方阵顺时针旋转。



void rotate(int* x, int rank)  
{  
    int* y = (int*)malloc(___________________);  // 填空  
  
    for(int i=0; i<rank * rank; i++)  
    {  
        y[_________________________] = x[i];  // 填空  
    }  
  
    for(i=0; i<rank*rank; i++)  
    {  
        x[i] = y[i];  
    }  
  
    free(y);  
}  
  
int main(int argc, char* argv[])  
{  
    int x[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};  
    int rank = 4;  
  
    rotate(&x[0][0], rank);  
  
    for(int i=0; i<rank; i++)  
    {  
        for(int j=0; j<rank; j++)  
        {  
            printf("%4d", x[i][j]);  
        }  
        printf("\n");  
    }  
  
    return 0;  
}  

答案:

rank*rank*sizeof(x)
(rank-1-i/rank)+rank*(i%rank)


6. (9')大数乘法

    对于32位字长的机器,大约超过20亿,用int类型就无法表示了,我们可以选择int64类型,但无论怎样扩展,固定的整数类型总是有表达的极限!如果对超级大整数进行精确运算呢?一个简单的办法是:仅仅使用现有类型,但是把大整数的运算化解为若干小整数的运算,即所谓:“分块法”。
    如图【1.jpg】表示了分块乘法的原理。可以把大数分成多段(此处为2段)小数,然后用小数的多次运算组合表示一个大数。可以根据int的承载能力规定小块的大小,比如要把int分成2段,则小块可取10000为上限值。注意,小块在进行纵向累加后,需要进行进位校正。


    以下代码示意了分块乘法的原理(乘数、被乘数都分为2段)。


void bigmul(int x, int y, int r[])  
{  
    int base = 10000;  
    int x2 = x / base;  
    int x1 = x % base;   
    int y2 = y / base;  
    int y1 = y % base;   
  
    int n1 = x1 * y1;   
    int n2 = x1 * y2;  
    int n3 = x2 * y1;  
    int n4 = x2 * y2;  
  
    r[3] = n1 % base;  
    r[2] = n1 / base + n2 % base + n3 % base;  
    r[1] = ____________________________________________; // 填空  
    r[0] = n4 / base;  
      
    r[1] += _______________________;  // 填空  
    r[2] = r[2] % base;  
    r[0] += r[1] / base;  
    r[1] = r[1] % base;  
}  
  
  
int main(int argc, char* argv[])  
{  
    int x[] = {0,0,0,0};  
  
    bigmul(87654321, 12345678, x);  
  
    printf("%d%d%d%d\n", x[0],x[1],x[2],x[3]);  
  
    return 0;  
}  
请分析代码逻辑,并推测划线处的代码。
答案写在 “解答.txt” 文件中

注意:只写划线处应该填的内容,划线前后的内容不要抄写。

答案:n2 / base + n3 / base + n4 % base
           r[2] / base


7. (13')放棋子

    今有 6 x 6 的棋盘格。其中某些格子已经预先放好了棋子。现在要再放上去一些,使得:每行每列都正好有3颗棋子。我们希望推算出所有可能的放法。下面的代码就实现了这个功能。
    初始数组中,“1”表示放有棋子,“0”表示空白。    


int N = 0;  
  
bool CheckStoneNum(int x[][6])  
{  
    for(int k=0; k<6; k++)  
    {  
        int NumRow = 0;  
        int NumCol = 0;  
        for(int i=0; i<6; i++)  
        {  
            if(x[k][i]) NumRow++;      
            if(x[i][k]) NumCol++;  
        }  
        if(_____________________) return false;  // 填空  
    }  
    return true;  
}  
  
int GetRowStoneNum(int x[][6], int r)  
{  
    int sum = 0;  
    for(int i=0; i<6; i++)   if(x[r][i]) sum++;  
    return sum;  
}  
  
int GetColStoneNum(int x[][6], int c)  
{  
    int sum = 0;  
    for(int i=0; i<6; i++)   if(x[i][c]) sum++;  
    return sum;  
}  
  
void show(int x[][6])  
{  
    for(int i=0; i<6; i++)  
    {  
        for(int j=0; j<6; j++) printf("%2d", x[i][j]);  
        printf("\n");  
    }  
    printf("\n");  
}  
  
void f(int x[][6], int r, int c);  
  
void GoNext(int x[][6],  int r,  int c)  
{  
    if(c<6)  
        _______________________;   // 填空  
    else  
        f(x, r+1, 0);  
}  
  
void f(int x[][6], int r, int c)  
{  
    if(r==6)  
    {  
        if(CheckStoneNum(x))  
        {  
            N++;  
            show(x);  
        }  
        return;  
    }  
  
    if(______________)  // 已经放有了棋子  
    {  
        GoNext(x,r,c);  
        return;  
    }  
      
    int rr = GetRowStoneNum(x,r);  
    int cc = GetColStoneNum(x,c);  
  
    if(cc>=3)  // 本列已满  
        GoNext(x,r,c);    
    else if(rr>=3)  // 本行已满  
        f(x, r+1, 0);     
    else  
    {  
        x[r][c] = 1;  
        GoNext(x,r,c);  
        x[r][c] = 0;  
          
        if(!(3-rr >= 6-c || 3-cc >= 6-r))  // 本行或本列严重缺子,则本格不能空着!  
            GoNext(x,r,c);    
    }  
}  
  
int main(int argc, char* argv[])  
{  
    int x[6][6] = {  
        {1,0,0,0,0,0},  
        {0,0,1,0,1,0},  
        {0,0,1,1,0,1},  
        {0,1,0,0,1,0},  
        {0,0,0,1,0,0},  
        {1,0,1,0,0,1}  
    };  
  
    f(x, 0, 0);  
      
    printf("%d\n", N);  
  
    return 0;  
}  

答案:

NumRow != 3||NumCol != 3
f(x,r,c+1)
x[r,c]


8. (10')密码发生器

    在对银行账户等重要权限设置密码的时候,我们常常遇到这样的烦恼:如果为了好记用生日吧,容易被破解,不安全;如果设置不好记的密码,又担心自己也会忘记;如果写在纸上,担心纸张被别人发现或弄丢了...
    这个程序的任务就是把一串拼音字母转换为6位数字(密码)。我们可以使用任何好记的拼音串(比如名字,王喜明,就写:wangximing)作为输入,程序输出6位数字。
    变换的过程如下:

    第一步. 把字符串6个一组折叠起来,比如wangximing则变为:
    wangxi
    ming 
    第二步. 把所有垂直在同一个位置的字符的ascii码值相加,得出6个数字,如上面的例子,则得出:
    228 202 220 206 120 105
    第三步. 再把每个数字“缩位”处理:就是把每个位的数字相加,得出的数字如果不是一位数字,就再缩位,直到变成一位数字为止。例如: 228 => 2+2+8=12 => 1+2=3

    上面的数字缩位后变为:344836, 这就是程序最终的输出结果!
    要求程序从标准输入接收数据,在标准输出上输出结果。
    输入格式为:第一行是一个整数n(<100),表示下边有多少输入行,接下来是n行字符串,就是等待变换的字符串。
    输出格式为:n行变换后的6位密码。

    例如,输入:
5
zhangfeng
wangximing
jiujingfazi
woaibeijingtiananmen
haohaoxuexi

    则输出:
772243
344836
297332
716652
875843

    注意:
    请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
    在评卷时使用的输入数据与试卷中给出的实例数据可能是不同的。
    请把所有函数写在同一个文件中,调试好后,存入与【考生文件夹】下对应题号的“解答.txt”中即可。
    相关的工程文件不要拷入。
    源代码中不能能使用诸如绘图、Win32API、中断调用、硬件操作或与操作系统相关的API。
    允许使用STL类库,但不能使用MFC或ATL等非ANSI c++标准的类库。例如,不能使用CString类型(属于MFC类库)。

分析:模拟即可

代码:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N = 10005;
char name[10][6];
int num[6];
int processnum(int n){
	int temp = 0;
	while(n/10){
		temp+=(n%10);
		n/=10;
	}
	temp+=n;
	if(temp/10) processnum(temp);
	else return temp;
}
void processascii(){
	//初始化
	for(int i = 0 ;i<10;i++){
		for(int j =0;j<6;j++){
			name[i][j] = NULL;
		}
	}
	memset(num,0,sizeof(num));
	//输入字符串
	scanf("%s",&name); 
	//将字符串处理成数字,存入num数组
	for(int j =0;j<6;j++){
		for(int i =0;i<10;i++){
			num[j] += (name[i][j] - NULL);
		}
	}
	//进行缩位处理
	for(int i =0;i<6;i++){
		num[i] = processnum(num[i]);
	} 
	return;
}

int main() {
	int n;cin>>n;
	while(n--){
		processascii();
		for(int i =0;i<6;i++) cout<<num[i];
		cout<<endl;
	}
	return 0;
}



9. (17')夺冠概率

    足球比赛具有一定程度的偶然性,弱队也有战胜强队的可能。
    假设有甲、乙、丙、丁四个球队。根据他们过去比赛的成绩,得出每个队与另一个队对阵时取胜的概率表:

    甲  乙  丙  丁   
甲   -  0.1 0.3 0.5
乙 0.9  -   0.7 0.4 
丙 0.7  0.3 -   0.2
丁 0.5  0.6 0.8 -

    数据含义:甲对乙的取胜概率为0.1,丙对乙的胜率为0.3,...
    现在要举行一次锦标赛。双方抽签,分两个组比,获胜的两个队再争夺冠军。(参见【1.jpg】)


    请你进行10万次模拟,计算出甲队夺冠的概率。

    注意:
    请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
    在评卷时使用的输入数据与试卷中给出的实例数据可能是不同的。
    请把所有函数写在同一个文件中,调试好后,存入与【考生文件夹】下对应题号的“解答.txt”中即可。
    相关的工程文件不要拷入。
    源代码中不能能使用诸如绘图、Win32API、中断调用、硬件操作或与操作系统相关的API。
    允许使用STL类库,但不能使用MFC或ATL等非ANSI c++标准的类库。例如,不能使用CString类型(属于MFC类库)。


分析:

甲获胜共有以下三种情况

将三种情况获胜概率分别求出

然后使用rand()函数模拟比赛进行10000次甲队的获胜概率


代码:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N = 10000;
double p[3];
int main() {
	p[0] = 0.1*0.8*0.5+0.1*0.2*0.3;
	p[1] = 0.3*0.6*0.5+0.3*0.4*0.1;
	p[2] = 0.5*0.7*0.1+0.5*0.3*0.3;
	double sum = 0;
	srand(time(NULL));
	for(int i =0;i<N;i++){
		int temp = rand()%3;
		sum+=p[temp];
	}
	cout<<(sum/10000)<<endl;
	return 0;
}


10. (25')取球游戏

    今盒子里有n个小球,A、B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个,也可以看到盒中还剩下多少个,并且两人都很聪明,不会做出错误的判断。
    我们约定:
    每个人从盒子中取出的球的数目必须是:1,3,7或者8个。
    轮到某一方取球时不能弃权!
    A先取球,然后双方交替取球,直到取完。
    被迫拿到最后一个球的一方为负方(输方)
    请编程确定出在双方都不判断失误的情况下,对于特定的初始球数,A是否能赢?
    程序运行时,从标准输入获得数据,其格式如下:
    先是一个整数n(n<100),表示接下来有n个整数。然后是n个整数,每个占一行(整数<10000),表示初始球数。
    程序则输出n行,表示A的输赢情况(输为0,赢为1)。
    例如,用户输入:
4
1
2
10
18

    则程序应该输出:
0
1
1
0

    注意:
    请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
    在评卷时使用的输入数据与试卷中给出的实例数据可能是不同的。
    请把所有函数写在同一个文件中,调试好后,存入与【考生文件夹】下对应题号的“解答.txt”中即可。
    相关的工程文件不要拷入。
    源代码中不能能使用诸如绘图、Win32API、中断调用、硬件操作或与操作系统相关的API。
    允许使用STL类库,但不能使用MFC或ATL等非ANSI c++标准的类库。例如,不能使用CString类型(属于MFC类库)。

分析:博弈论取球问题

代码:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
const int N = 10005;
int d[N] = {0,0,1,0,1,0,1,0,1};
int take[4] = {1,3,7,8};
void init(){
	for(int i =9;i<N;i++){
		for(int j = 0;j<4;j++){
			if(d[i - take[j]] == 0){
				d[i] = 1;
				break;
			}
		}
	}
}
int main() {
	init();
	int n;cin>>n;
	while(n--){
		int x;cin>>x;
		cout<<d[x]<<endl;
	}
	return 0;
}


void rotate(int* x, int rank)  
{  
    int* y = (int*)malloc(___________________);  // 填空  
  
    for(int i=0; i<rank * rank; i++)  
    {  
        y[_________________________] = x[i];  // 填空  
    }  
  
    for(i=0; i<rank*rank; i++)  
    {  
        x[i] = y[i];  
    }  
  
    free(y);  
}  
  
int main(int argc, char* argv[])  
{  
    int x[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};  
    int rank = 4;  
  
    rotate(&x[0][0], rank);  
  
    for(int i=0; i<rank; i++)  
    {  
        for(int j=0; j<rank; j++)  
        {  
            printf("%4d", x[i][j]);  
        }  
        printf("\n");  
    }  
  
    return 0;  
}  

void rotate(int* x, int rank)  
{  
    int* y = (int*)malloc(___________________);  // 填空  
  
    for(int i=0; i<rank * rank; i++)  
    {  
        y[_________________________] = x[i];  // 填空  
    }  
  
    for(i=0; i<rank*rank; i++)  
    {  
        x[i] = y[i];  
    }  
  
    free(y);  
}  
  
int main(int argc, char* argv[])  
{  
    int x[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};  
    int rank = 4;  
  
    rotate(&x[0][0], rank);  
  
    for(int i=0; i<rank; i++)  
    {  
        for(int j=0; j<rank; j++)  
        {  
            printf("%4d", x[i][j]);  
        }  
        printf("\n");  
    }  
  
    return 0;  
}  

void rotate(int* x, int rank)  
{  
    int* y = (int*)malloc(___________________);  // 填空  
  
    for(int i=0; i<rank * rank; i++)  
    {  
        y[_________________________] = x[i];  // 填空  
    }  
  
    for(i=0; i<rank*rank; i++)  
    {  
        x[i] = y[i];  
    }  
  
    free(y);  
}  
  
int main(int argc, char* argv[])  
{  
    int x[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};  
    int rank = 4;  
  
    rotate(&x[0][0], rank);  
  
    for(int i=0; i<rank; i++)  
    {  
        for(int j=0; j<rank; j++)  
        {  
            printf("%4d", x[i][j]);  
        }  
        printf("\n");  
    }  
  
    return 0;  
}  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值