Baidu interview question"
There is a 27 cm-long fine wooden pole. There is an ant in each of the five positions 3 cm, 7 cm, 11 cm, 17 cm, 23 cm. The wooden pole is very fine and cannot pass through two ants at the same time, at the beginning, the head of the ant financial system is arbitrary to the left or right. They only move forward or turn around, but do not move back. When one or two ants meet, at the same time, the ant will turn around and go in the opposite direction, assuming that the ant can walk 1 cm away every second.
WriteProgram(C/C ++) to find the minimum and maximum time for all ants to leave the wooden pole.
# Include <iostream>
# Include <string>
# Include <cmath>
Using STD: cout;
Using STD: Endl;
# Define slen 27
Int getl (int ad)
{
Return ABS (slen-AD)> ad? ABS (slen-AD): ad;
};
Int gets (int ad)
{
Return slen-getl (AD );
};
Int main (void)
{
Int ads [] = {3, 7, 11, 17, 23 };
Int L = 0, S = 0;
For (INT I = 0; I <5; ++ I)
{
If (L <getl (ADS [I]) L = getl (ADS [I]);
If (S <gets (ADS [I]) S = gets (ADS [I]);
};
Cout <"maximum" <L <"seconds. <S <"seconds required for \ n shortest. "<Endl; return 0;
}