From: ysuncn
After reading Baidu's questions, many people were interested in learning about it. For about an hour, I wrote C ++ to join the crowd. It was very unstandard. I smiled and looked at it. Algorithm ----------------:)
There is a 27 cm fine wood pole, each of which has an ant in the five positions 3 cm, 7 cm, 11 cm, 17 cm, 23 cm. The wooden pole is very small and cannot pass through an ant at the same time. At the beginning, the ant financial's head is left or right. They only move forward or turn their heads, but do not move back. When any two ants meet, they both turn their heads to the opposite direction. It is assumed that the ant Financial can walk 1 cm away every second. WriteProgramThe minimum time and maximum time for all ants to leave the wooden pole.
# Include <iostream>
Using namespace STD;
Class ant
{
Public:
Bool Orient; // 0-left; 1-right;
Short Pos; // 0-27;
Bool onbar; // 0-off; 1-on;
Void meet ()// -------------------- Reverse MEETING
{
Orient = Orient> 0? ;
}
Void move () // -------------------- Not to the endpoint, move in the direction, to mark
{
If (Orient = 0) pos --;
Else POS ++;
If (Pos = 0 | Pos = 27) onbar = 0;
}
};
Int main ()
{
Ant A1, A2, A3, A4, A5;
Cout <"Orient (0-left; 1-Right)" <Endl;
For (INT I1 = 0; I1 <= 1; I1 ++)
For (INT I2 = 0; I2 <= 1; I2 ++)
For (INT I3 = 0; I3 <= 1; I3 ++)
For (INT I4 = 0; I4 <= 1; I4 ++)
For (INT I5 = 0; I5 <= 1; I5 ++)
{// -------------------- Initialize five ant financial
A1.orient = I1; a2.orient = I2; a3.orient = I3; a4.orient = I4; a5.orient = I5;
A1.pos = 3; a2.pos = 7; a3.pos = 11; a4.pos = 17; a5.pos = 23;
A1.onbar = 1; a2.onbar = 1; a3.onbar = 1; a4.onbar = 1; a5.onbar = 1;
Cout <"Orient:" <a1.orient <a2.orient <a3.orient <a4.orient <a5.orient <"";
Short time = 0;
While (a1.onbar | a2.onbar | a3.onbar | a4.onbar | a5.onbar)
{// -------------------- On the pole, the ant crawls for one second to determine the encounter
If (a1.onbar) a1.move ();
If (a2.onbar) a2.move ();
If (a3.onbar) a3.move ();
If (a4.onbar) a4.move ();
If (a5.onbar) a5.move ();
If (a1.pos + 1 = a2.pos & a1.onbar & a2.onbar) {a1.meet (); a2.meet ();}
If (a2.pos + 1 = a3.pos & a2.onbar & a3.onbar) {a2.meet (); a3.meet ();}
If (a3.pos + 1 = a4.pos & a3.onbar & a4.onbar) {a3.meet (); a4.meet ();}
If (a4.pos + 1 = a5.pos & a4.onbar & a5.onbar) {a4.meet (); a5.meet ();}
Time ++;
}
Cout <"cost time:" <time <"S" <Endl;
}
}
result:
Orient (0-left; 1-Right)
Orient: 00000 cost time: 23 S
Orient: 00001 cost time: 17 S
Orient: 00010 cost time: 23 S
Orient: 00011 cost time: 11 S
Orient: 00100 cost time: 23 S
Orient: 00101 cost time: 17 S
Orient: 00110 cost time: 23 S
Orient: 00111 cost time: 16 S
Orient: 01000 cost time: 23 S
Orient: 01001 cost time: 20 S
Orient: 01010 cost time: 23 S
Orient: 01011 cost time: 20 S
Orient: 01100 cost time: 23 S
Orient: 01101 cost time: 20 S
Orient: 01110 cost time: 23 S
Orient: 01111 cost time: 20 S
Orient: 10000 cost time: 24 S
Orient: 10001 cost time: 24 S
Orient: 10010 cost time: 24 S
Orient: 10011 cost time: 24 S
Orient: 10100 cost time: 24 S
Orient: 10101 cost time: 24 S
Orient: 10110 cost time: 24 S
Orient: 10111 cost time: 24 S
Orient: 11000 cost time: 24 S
Orient: 11001 cost time: 24 S
Orient: 11010 cost time: 24 S
Orient: 11011 cost time: 24 S
Orient: 11100 cost time: 24 S
Orient: 11101 cost time: 24 S
Orient: 11110 cost time: 24 S
Orient: 11111 cost time: 24 S
tips!