The third Blue Bridge Cup real title C + +
The following topics I am not all the topics are a one-time can do or have a problem thinking. Some of the topics are also verified on-line related information or reference to other People's Code and the idea of the Problem. overall, This topic tests a lot of loops just, there are simulations, dynamic planning is Just.
In the process of doing the problem, there are some new experiences. At first, I did not know what to do when the code is right or wrong, ran to the Internet to check the answer, and later found that the topic has given the code, the beginning we can add their own header file, and then the vacancy code plus debugging, so you can verify that the code is Correct.
In addition, when debugging, you can redirect the input output to a file using redirection, for Example:
Input redirection: freopen ("testin.txt", "r", Stdin)
Output redirection: freopen ("testout.txt", "w", stdout);
After redirection, there is a large number of input data or a large number of output data when the output test data time is omitted, or is more Convenient. however, It is important to note that the re-programming is required to submit the code when you must remember to comment out the redirect statement or delete, the person will be wrong.
As a result, our algorithm is not necessarily excellent, and many times it is possible to get answers with brute force solutions.
The following topics I have just done a simple test, although the test results are correct. But there is no guarantee that the answer is correct, so if the wrong place can help to correct it ...
"1" Microbial Value Added
Problem
Suppose there are two kinds of microbes X and Y
X is split once every 3 minutes after birth (doubling the number), and y is split every 2 minutes after birth (doubling the number).
A new-born x eats 1 y in half a minute, and starts with 1 y every 1 minutes.
There are now known to be New-born x=10, y=89, for 60 minutes after the number of Y.
What if x=10,y=90?
The requirement is to write down the number of Y in these two initial conditions after 60 minutes.
Did the result of the question surprise you? This is not a simple number game! The real biosphere has the same fragile nature! Maybe because the y you wiped out was the last straw that eventually led to the extinction of the Y population!
Please refrain from sadness, write the answer in the "answer. txt", do not write here!
Analytical
The time can be 0.5 minutes to step, layered 6 stages, The x in each phase of the group x[7], where x[i] represents the number of x 0.5*i minutes after splitting, the period is 6.
The X of x[1], x[3], x[5] will eat y, while the X of x[6] will increment, and the value of x goes to state x[1] to start the new cycle again. After 60 minutes, both 120 and a half minutes later, and y splits every 2 minutes, both for every 4 and half minutes.
Answer
0
94371840
Reference Code
#include <stdio.h> #include <stdlib.h> #define M 1000int main () { long long xn,yn,newx,x[7]; int s=120,i,j; scanf ("%d%d", &xn,&yn); For (i=0;i<7;i++) x[i]=0; x[1]=xn; For (i=1;i<=s;i++) { yn=yn-x[1]-x[3]-x[5];//x eat y if (yn<=0) {yn=0;break;} Y is finished eating newx=2*x[6];//x value -added for (j=6;j>1;j--) x[j]=x[j-1];//x State transfer x[1]=newx; If (i%4==0) yn=2*yn;//y Value added } printf ("%lld\n", yn);
"2" Castle Formula
Problem
Sherlock Holmes went to an expedition to the castle and saw a strange expression written on the Door:
ABCDE *? = EDCBA
He said to Watson: "the ABCDE should represent different numbers, and the question mark also represents a number!" ”
Watson: "i Guess so!" ”
so, Two people silent for a long time, still did not calculate the suitable result.
Please take advantage of the computer to find the answer to the CRACK.
Write out the figures represented by ABCDE.
The answer is written in the "answer. txt", do not write here!
? 0-9
A 0-9
E 0-9
B 0-9
C 0-9
D 0-9
E 0-9
Analysis
There are a total of 7 different symbols in the subject, which can be solved by using 7 nested loops to solve the problem of the Equation.
Answer
A 2
B 1
C 9
D 7
E 8
? 4
Open code
/* summary: sometimes, If allowed, you can use the exhaustive method to Solve. In addition, there is no judging condition that can be in the for (;;) The middle of the loop, where the first empty initial conditions, the second condition is the condition to jump out of the loop, and the last null is the iteration Condition. */#include <stdio.h> #include <stdlib.h>int main () {int a,b,c,d,e,f,i=0; For (A=0;A<=9;A++)//2 {for (b=0;b<=9;b++)//1 {if (b==a) continue; For (C=0;C<=9;C++)//9 {if (c==a| | C==b) continue; For (D=0;D<=9;D++)//7 {if (d==a| | d==b| | D==c) continue; For (E=0;E<=9;E++)//8 {if (e==a| | e==b| | e==c| | E==d) continue; For (F=0;F<=9;F++)//4 {if (f==a| | f==b| | f==c| | f==d| | F==e) continue; If ((a*10000+b*1000+c*100+d*10+e) *f== (e*10000+d*1000+c*100+b*10+a)) { printf ("%d%d%d%d%d%d", a,b,c,d,e,f); } }}}}}} return 0;}
"3" is more than Liquor.
Problem
There are a group of pirates (not more than 20 people), on the boat to compete for Alcohol. The process is as follows: open a bottle of wine, and all the people present were equally drunk, and several people fell down. Then open a bottle of wine equally, and have fallen, repeat again ... Until the 4th bottle of wine was opened, there was nothing left to sit on, and the pirate captain was There. When the 4th bottle of wine was equally drunk, everyone fell down.
When the captain woke up, he found the pirate ship Stranded. He wrote in the Logbook: "... yesterday, I just drank a bottle of ... We advise you not to drink, drink or sail ... "
Based on this information, please infer how many people are starting, and how many people are left to drink each round.
If there are multiple possible answers, list all the answers, one for each answer.
The format is: number of people, number,...
For example, one might be: 20,5,4,2,0
The answer is written in the "answer. txt", do not write here!
Analysis
The problem can be solved with 4 nested loops, each of which has a control variable that is the number of people before drinking the first bottle of Wine. The key to solving the problem here is: 1, each drink is a score, 2 the captain drank the fourth bottle after drinking before the fall, and the captain and a total of a bottle of Wine.
Answer
12 6 4) 2 0
15 10 3) 2 0
18 9 3) 2 0
20 5 4) 2 0
Reference Code
#include <stdio.h>int main () { //freopen ("result.txt", "w+", stdout); int p,i,j,k; For (p=4;p<=20;p++)//first bottle before number {for (i=1;i<p;i++)//second bottle before number {for (j=1;j<i;j++)//third bottle of wine before number { for (k=1;k<j;k++)//fourth bottle of wine before the number {float Num=1.0/p+1.0/i+1.0/j+1.0/k;//captain drank a bottle of wine if (num==1) { printf ("%d%d%d%d\n", p,i,j,k,0);} } } }
"4" Strange Game
Problem
A TV station held a Low-carbon life grand Prix. The scoring rules of the topic are rather strange:
Each player needs to answer 10 questions (numbered 1 to 10), and the later is more Difficult. correct, the current score doubled, the wrong answer is deducted with the same number of points (the player must answer the question, do not answer by mistake).
Each player has a starting score of 10 Points.
A winner will score exactly 100 points, if you do not let you see the game process, you can infer that he (she) which topic is correct, which question answer wrong?
If the correct answer is recorded as 1, the wrong answer is 0, then 10 questions can be answered with only 1 and 0 of the string to Express. For Example: 0010110011 is a possible situation.
Your task is to figure out all the possible situations. Each answer takes up one line.
The answer is written in the "answer. txt", do not write here!
Analysis
The simplest idea is that there are ten nested loops, and no punch loops represent a set of questions. finally, Select the answer output that satisfies the Condition. But the cycle of the Ten is a bit more, it is difficult to find errors, the beginning there are many times recursion and circulation are interchangeable.
And this problem, each answer a question is independent, and each problem only two cases, correct answer or wrong answer. void Fun (int pos,int fen), recursive function, pos is the problem number, and Fen is the current score, the recursive exit is when pos==10
When the last problem is reached, then you can check the existing fen and the current 10th question of the answer score to calculate the sum, whether to get 100 points, if the output results, no processing, and finally backtracking. If pos! =10 should call the recursive function in two different cases.
Answer
0010110011
0111010000
1011010000
See Code
/* If you use 10 nested loops, The amount of code is very large and error-prone and not easy to check. if it can be implemented recursively, the code is introduced, and errors are easily found. so when doing the problem should think more about the method of solving problems, if the first immediately into the head into the multi-nested loop implementation, it is possible to spend more Time debugging than the encoding Time. */#include <stdio.h>int x[11];void display () { int i; For (i=1;i<=10;i++) { printf ("%d", x[i]); } printf ("\ n");} void Fun (int pos,int fen) { if (pos==10) { if ((FEN-POS) ==100) { x[pos]=0; Display (); } If ((fen*2) ==100) { x[pos]=1; Display (); } } else { x[pos]=0; Fun (pos+1,fen-pos); x[pos]=1; Fun (pos+1,fen*2);} } int main () {fun (1,10);
"5" turn Phalanx
Problem
To a square matrix transpose, is to change the original line number of the column number, the original column number of the line number
For example, the following phalanx:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
After the transpose becomes:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
however, If the square is rotated clockwise (not transpose), it is the following result:
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
The following code implements the function of turning a square into a clockwise Rotation.
void rotate (int* x, int rank)
{
Int* y = (int*) malloc (___________________); Blanks
For (int i=0; I<rank * rank; I++)
{
y[_________________________] = x[i]; Blanks
}
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;
}
Please analyze the code logic and speculate on the code at the Dash.
The answer is written in the "answer. txt" file
Note: write only what should be filled in the line, and do not copy the contents before or after the Dash.
Answer
(1)
Rank*rank*sizeof (int)
rank-i/rank-1+rank* (i%rank)
Or
(2)
Rank*rank*sizeof (int)
(i%rank+ (i%rank+1) * (rank-1))-i/rank
Experience
Code in the blanks, you can copy the code of the topic to the compilation environment for debugging, to further verify the correctness of the Results.
"6" large number multiplication
Problem
For a 32-bit word machine, about 2 billion, the int type cannot be represented, we can choose the int64 type, but no matter how the extension, fixed integer type always has the limit of expression! What if the super-large integers are accurately calculated? One simple approach is to use only existing types, but to dissolve the operations of large integers into small integers, the so-called "chunking method".
"1.jpg" represents the principle of block Multiplication. A large number can be divided into multiple segments (here, 2) decimals, and then a large number is represented by a combination of multiple operations of Decimals. You can specify the size of the small block according to the load capacity of int, for example, to divide an int into 2 segments, the small block may be 10000 as the upper limit Value. Note that a rounding adjustment is required after the small block has been vertically accumulated.
The following code shows the principle of block multiplication (multiplier, The multiplier is divided into 2 segments).
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] = ____________________________________________; Blanks
r[0] = n4/base;
r[1] + = _______________________; Blanks
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;
}
Please analyze the code logic and speculate on the code at the Dash.
The answer is written in the "answer. txt" file
Note: write only what should be filled in the line, and do not copy the contents before or after the Dash.
Tips
Copy the code into the compilation environment for debugging, and the results of the calculation can be verified correctly by comparing the computed demerit of the calculator in the computer Attachment.
Answer
N2/base+n3/base+n4%base
R[2]/base
"7" play chess pieces
Now there are 6 x 6 Checkers. Some of the squares have been pre-placed. Now it's time to put up some more, so that each row has exactly 3 pieces in each Column. We hope to deduce all possible methods of Release. The following code implements this Function.
In the initial array, "1" means a pawn is placed, and "0" indicates a blank.
Now there are 6 x 6 Checkers. Some of the squares have been pre-placed. Now it's time to put up some more, so that each row has exactly 3 pieces in each Column. We hope to deduce all possible methods of Release. The following code implements this Function.
In the initial array, "1" means a pawn is placed, and "0" indicates a blank.
int N = 0;
BOOL Checkstonenum (int X[][6])//check whether the rows of the board meet the requirements
{
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 (_______numrow!=3| | Numcol!=3______________) return false; Blanks
}
Return true;
}
int getrowstonenum (int x[][6], int r)//get The number of row flags
{
int sum = 0;
For (int i=0; i<6; i++) if (x[r][i]) sum++;
Return sum;
}
int getcolstonenum (int x[][6], int c)//get The number of flags in the column
{
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)
_f (x, r, c+1) ______________________; Blanks
Else
F (x, r+1, 0);
}
void f (int x[][6], int r, int C)
{
If (r==6)//has traversed to the last line
{
If (checkstonenum (x))
{
n++;
Show (x);
}
Return
}
If (_x[r][c]_____________)//the pawn is already in place
{
GoNext (x,r,c);
Return
}
int rr = Getrowstonenum (x,r);
int cc = Getcolstonenum (x,c);
If (cc>=3)//Benlie full
GoNext (x,r,c);
else if (rr>=3)//bank is full
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)//bank or this column serious missing son, then this grid can not be empty!
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;
}
Please analyze the code logic and speculate on the code at the Dash.
The answer is written in the "answer. txt" file
Note: write only what should be filled in the line, and do not copy the contents before or after the Dash.
Answer
numrow!=3| | Numcol!=3
F (x, r, C+1)
x[r][c]
"8" Password Generator
Problem
In the bank account and other important permissions to set the password, we often encounter such a problem: if you want to remember the birthday, easy to be cracked, unsafe, if you set a bad password, but also worried that they will forget, if written on paper, worried that the paper was found or lost ...
The task of this program is to convert a string of phonetic alphabet to a 6-digit number (password). We can use any well-remembered pinyin string (such as name, Wang Ximing, write: Wangximing) as input, the program output 6 Digits.
The process of transformation is as Follows:
The first Step. Fold the string 6 a group, for example, wangximing into:
Wangxi
Ming
The second Step. The ASCII values of all characters perpendicular to the same position are added, resulting in 6 numbers, as shown in the example above:
228 202 220 206 120 105
The third Step. Then the number of each digit "indent" processing: is to add the number of each bit, the figure if it is not a number, and then shrink, until it becomes a digit. For example: 228 = 2+2+8=12 = 1+2=3
The above number is reduced to: 344836, which is the final output of the program!
Requires the program to receive data from the standard input and output the results on the standard Output.
The input format is: the first line is an integer n (<100), which indicates how many input rows are below, followed by n-line strings, or strings waiting to be transformed.
The output format is: n-line 6-bit password after Transformation.
For example, Enter:
5
Zhangfeng
Wangximing
Jiujingfazi
Woaibeijingtiananmen
Haohaoxuexi
The Output:
772243
344836
297332
716652
875843
Attention:
Please carefully debug! Your program only has the chance to score when it can run the correct results!
The input data used in the grading is probably different from the instance data given in the quiz paper.
Please write all the functions in the same file, after debugging, deposited with the "candidate folder" corresponding to the title of the "answer. txt" can Be.
The relevant engineering documents should not be copied into.
The source code cannot use APIs such as drawing, win32api, interrupt invocation, hardware manipulation, or operating System-related.
STL class libraries are allowed, but not the class libraries of Non-ansi C + + standards such as MFC or ATL. For example, You cannot use the CString type (which belongs to the MFC class Library).
Analytical
This topic basically has given the procedure algorithm implementation steps, as long as the requirements and steps to write code, basically can solve the problem
Reference Code
/* experience: ceil ((double) len/6); note: LEN/6 is rounded down by default, only if it is cast to a double type and the result of the operation is preserved as a decimal, and the rounding function plays a role./#include <stdio.h># include<string.h> #include <math.h> #define M 10000char Input[m];char str[m][6];int resul[6];int n;int F (int A) {int sum=0; While (a) {sum+= (a%10); a=a/10; } return sum;} int main () {scanf ("%d", &n); int i,len; While (n--) {memset (str,0,sizeof (str)); scanf ("%s", &input); Len=strlen (input); For (i=0;i<len;i++) {str[i/6][i%6]=input[i]; } int Sum,j,row=ceil ((double) len/6); For (i=0;i<6;i++) {sum=0; For (j=0;j<row;j++) {sum+=str[j][i]; } resul[i]=sum; } for (i=0;i<6;i++) {while (1) {if (RESUL[I]>=0&&A Mp;resul[i]<=9) break; Resul[i]=f (resul[i]); }} for (I=0;i<6;i++) printf ("%d", resul[i]); printf ("\ n"); } return 0;}
"9" the probability of winning the army
Problem
Football matches have a certain degree of contingency, the weak team also has the possibility of defeating strong Teams.
Suppose there are four teams of a, b, c, ding. Based on the results of their past matches, the probability table for each team to win against the other team is as Follows:
Methyl n-propyl
armour-0.1 0.3 0.5
B 0.9-0.7 0.4
C 0.7 0.3-0.2
D 0.5 0.6 0.8-
Data meaning: A to B win probability is 0.1, the winning rate of C to B is 0.3, ...
It's time to hold a tournament. The two sides draw the lottery, divided into two groups than the winning two teams to compete for the Championship. (see "1.jpg")
Please do 100,000 simulations to calculate the probability of a team winning the Championship.
Attention:
Please carefully debug! Your program only has the chance to score when it can run the correct results!
The input data used in the grading is probably different from the instance data given in the quiz paper.
Please write all the functions in the same file, after debugging, deposited with the "candidate folder" corresponding to the title of the "answer. txt" can Be.
The relevant engineering documents should not be copied into.
The source code cannot use APIs such as drawing, win32api, interrupt invocation, hardware manipulation, or operating System-related.
STL class libraries are allowed, but not the class libraries of Non-ansi C + + standards such as MFC or ATL. For example, You cannot use the CString type (which belongs to the MFC class Library).
Analysis
This problem is mainly in the simulation algorithm, to use the Rand () function to generate random numbers, rand () and the rules of the game, basically solve the problem is not too much.
Reference Code
#include <stdio.h> #include <stdlib.h> #include <time.h>float rate[][6]={{0,1,3,5}, {9,0,7,4}, {7,3,0,2}, {5,6,8,0}};int main () {int a,a1,b,b1,i,count=0; Srand (time (NULL)), or//random weight in the for loop outside for (i=0;i<100000;i++) {a1=1+rand ()%3;//produces a random number from the side, which produces both a (a) of the opponent if (r and ()%10<rate[0][a1])//a wins; Rand ()%10, generates a random number of 0~9 {switch (a1) {case 1: b=2; b1=3; Break Case 2:b=1; b1=3; Break Case 3:b=1; b1=2; Break default:break; } If (rand ()%10<rate[b][b1])//b wins {a1=b; } else a1=b1; If (rand ()%10<rate[0][a1]) count++; }} printf ("%f\n", count*1.0/100000); RetUrn 0;}
"10" take the ball game
Problem
This box has n ball, a, b Two people take the ball in turn from the box, everyone can see how many other people have taken, also can see how many in the box, and both are very smart, will not make the wrong judgment.
We agree:
The number of balls that each person pulls out of the box must be: 1,3,7 or 8.
You cannot abstain from taking the ball on one side!
A take the ball first, then the two sides take the ball alternately, until the end is Taken.
The one who is forced to get the last ball is the negative side (the Loser)
Please program to determine if both sides do not judge the error, for a particular initial number of balls, a can win?
When the program runs, the data is obtained from standard input in the following format:
First an integer n (n<100), which indicates that there are n integers next. Then there are n integers, one for each row (integer <10000), representing the initial number of Balls.
The program outputs n lines, which indicates the winning and losing situation of a (lost to 0, winning is 1).
For example, user input:
4
1
2
10
18
The program should output:
0
1
1
0
Attention:
Please carefully debug! Your program only has the chance to score when it can run the correct results!
The input data used in the grading is probably different from the instance data given in the quiz paper.
Please write all the functions in the same file, after debugging, deposited with the "candidate folder" corresponding to the title of the "answer. txt" can Be.
The relevant engineering documents should not be copied into.
The source code cannot use APIs such as drawing, win32api, interrupt invocation, hardware manipulation, or operating System-related.
STL class libraries are allowed, but not the class libraries of Non-ansi C + + standards such as MFC or ATL. For example, You cannot use the CString type (which belongs to the MFC class Library).
Analysis
This is a dynamic programming problem, set flag[i] indicates that the initial ball number is I when the outcome of a, when the flag[i]==0 is negative, No in the expression win. Each time it was allowed to take the ball k={1,3,7,8}, in addition, apparently flag[1]=0.
Now requires the initial ball number is x, a of the outcome of the situation, and a first take the ball, then the first time to take the ball K, as long as check flag[x-k] the outcome of the situation can be known flag[x], if flag[x-k]==0 flag[x-k]=1, if the inspection of all K can not
If such a situation is found, the flag[x-k]=0 is Indicated.
Reference Code
#include <stdio.h> #include <string.h> #define M1 10010#define M2 110int main () { int n,i,j,max,a[m2], flag[m1],b[]={1,3,7,8}; Memset (flag,0,sizeof (flag));//can be initialized when flag is defined, since int flag[m1]={0}; scanf ("%d", &n); max=-1; For (i=1;i<=n;i++) { scanf ("%d", &a[i]); If (a[i]>max) max=a[i];//records the maximum number of initial balls } for (i=2;i<=max;i++)//dynamic plan {for (j=0;j<4& &b[j]<i;j++)//try all feasible ball-taking schemes, both the number of balls to take away must be less than the number of balls currently held { if (flag[i-b[j]]==0)///take b[j] after the ball State if it is lost now a take b[j] A ball is to win { flag[i]=1; Break ; }}} For (i=1;i<=n;i++) { printf ("%d\n", flag[a[i]]); } Return 0;}
The third Blue Bridge Cup C + + real problem