Baidu interview questions [transferred from csdn]

Source: Internet
Author: User

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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.