The 4th C language Blue Bridge Cup real questions and answers, the 4th Blue Bridge Cup C/C++ C group finals in 2013

1. Title: Study hard

Tom and his grandfather are traveling to China. One day, he helped Chinese children put up slogans. The slogan he is responsible for sticking is four big characters written on four pieces of red paper: "good, good, learning, learning". But Tom didn't know Chinese characters, so he wanted to stick it in a random line.

Please do the math for little Tom, what is the probability that he just posted it right?

The answer is a fraction, expressed as the ratio of two whole numbers. For example: 1/3 or 2/15 etc.

If possible, please output the reduced result.

Note: Do not write extra spaces.

Please follow the format strictly and submit your answer through your browser.

Note: Submit only this ratio, do not write other additional content, such as: descriptive text.

[Analysis] Permutation and Combination Problems Full Permutation + Enumeration

#include

#include

using namespace std;

intmain()

{

int a[4]={1,1,2,3}; //1-good 2-learning 3-learning

int cnt=0,tot=0; //cnt- the number of correct cases tot- the total number of possible cases

do

{

to++;

cost<

if(a[0]==1 && a[1]==1 && a[2]==2 && a[3]==3)

cnt++;

} while(next_permutation(a,a+4));

cost<

return 0;

}

【Answer】1/12

2. Title: Egyptian Score

Ancient Egypt once created a splendid human civilization, but their scores are very puzzling. Ancient Egypt liked to decompose a fraction into something like: 1/a + 1/b.

Here, a and b must be two different integers, and the numerator must be 1

For example, 2/15 has 4 different decomposition methods (let's call it the Egyptian decomposition method):

1/8 + 1/120

1/9 + 1/45

1/10 + 1/30

1/12 + 1/20

So, how many different Egyptian decompositions are there in 2/45 (those that satisfy the commutative law of addition count as the same decomposition)? Please submit the integer directly (never submit a detailed decomposition!).

Please submit answers through your browser strictly as required.

Note: only submit the number of categories of decomposition, do not write other additional content, such as: descriptive text

[Analysis] Loop + Enumeration

分析此题,因为要求将分数2/45拆成两项,分子为1,两个分数的分母为a和b,因此可采用枚举法,枚举所有可能情况(这里取a=45时是无解的,因为要求a和b必须是不同的两个整数。

#include

#include

int main()

{

int cnt=0;

int a,b;

double left,right;

for(a=2;a<45;a++)

{

for(b=a+1;b<=10000000;b++)

{

left=1.0/a+1.0/b;

right=2.0/45;

if(fabs(left-right)<1e-8)

{

printf("1/%d+1/%d=2/45\n",a,b);

cnt++;

}

}

}

printf("%d\n",cnt);

return 0;

}

【答案】7

3. 标题:正负金字塔

看下面的图形:

+ - + - - + - + - - + - - + -

- - - + - - - - + - - + - -

+ + - - + + + - - + - - +

+ - + - + + - + - - + -

- - - - + - - - + - -

+ + + - - + + - - +

+ + - + - + - + -

+ - - - - - - -

- + + + + + +

- + + + + +

- + + + +

- + + +

- + +

- +

-

它是由正号和负号组成的金字塔形状。其规律是:每个符号的左上方和右上方符号如果相同,则输出为正号,否则为负号。其第一行数据由外部输入。

以下代码实现了该功能。请仔细阅读代码,并填写划线部分缺失的代码。

【分析】字符处理问题

要注意填空位置等号左边的x[i]是当前行的字符,等号右边的x[i]和x[i+1]是上一行的字符,即当前位置字符的左上方和右上方字符。(PS:题目有一个特别特别特别坑的地方,测试函数中的test应改为main,否则无法编译。。)

#include

void f(char* x, int space, int n)

{

int i;

if(n<1) //所打印行的字符数为0,直接返回,结束

return;

for(i=0; i

printf(" ");

for(i=0; i

printf("%c ", x[i]);

printf("\n");

for(i=0; i

x[i] =(x[i]==x[i+1])?'+':'-';

f(x,space+1,n-1);

}

// 用于f的测试

int main()

{

char x[] = "+-+--+-+--+--+-"; //第一行数据由外部输入

//char x[] = "+-+";

f(x, 5, sizeof(x)-1);

return 0;

}【答案】(x[i]==x[i+1])?'+':'-'



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325664337&siteId=291194637