Ant problems on wooden poles

Source: Internet
Author: User
Tags strtok

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 head of the ant financial system is either left-facing or right-facing. They only move forward or turn their heads,
But will not go 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.
Find the minimum and maximum time for all ants to leave the wooden pole.

Length of input wooden pole L
Number of input ants n
Enter the positions of each ant on the wooden pole x1, x2 ...... Xn
Output the minimum and maximum time for all ants to leave the wooden pole.
The following is my solution
 
/*
Gcc test. c-o test
./Test 27 5, 7, 11, 17, 23
 
Assume that there are only two ant A and B
A is at section 3 cm a and B is at section 7 cm B.
 
 
If A and B are opposite
0 3 5 7 27
+ --- + -- + --------------------------------------------------- +
X a o B y
A-> <-B
 
Then, after A and B meet at the 5 cm o, they turn their heads to the opposite direction.
Then A's journey is ao + ox = 7
B's journey is bo + oy = 20
Then A + B = ao + ox + bo + oy = (ao + oy) + (bo + ox) = ay + bx
Found
When any two ants meet, both of them turn their heads to the opposite direction at the same time = when any two ants meet, the two ants will switch their locations and will not move forward.
Therefore, when calculating the maximum and minimum time, you can ignore
When any two ants meet, they both turn their heads toward the opposite direction.
The shortest time, A goes to the end closest to her, that is, 3, B goes to the end closest to her, that is, 7
When the time is the most, A goes to the farthest end of her, that is, 27-3. B goes to the farthest end of her, that is, 27-7.
 
When the number of ants is n, the shortest time is reached. All the ants are taking the closest one to her. The biggest ant is usually the one in the middle.
When the number of ants is n and the time is the most, all the ants are walking the farthest end from her. The ant with the greatest distance is usually the one closest to the two ends.
 
* System environment: windows/linux
* Compiling environment: gcc/vc ++ 6.0
* Input parameters: multiple parameters are separated by space spaces.
Parameter 1 is a set of numbers
Parameter 2 is a number
Parameter 3 is a group of numbers separated by commas
Example format: 27 5, 7, 11, 17, 23
*/
# Include <ctype. h>
# Include <string. h>
# Include <stdlib. h>
# Include <stdio. h>
Int max (int a, int B)
{
Return a> B? A: B;
}
Int min (int a, int B)
{
Return a> B? B:;
}
Int main (int argc, char ** argv)
{
Int L = 0;
Int n = 0;
Int speed = 1;
Char * p;
Char * beakChar = ",";
Int minTime = 0;
Int maxTime = 0;
If (argc! = 4)
{
Printf ("params number must equal 4 ");
}
L = atoi (argv [1]);
N = atoi (argv [2]);
Strtok (argv [3], beakChar );
While (p = strtok (NULL, beakChar )))
{
Int position = atoi (p );
MinTime = max (minTime, min (position, L-position ));
MaxTime = max (maxTime, max (position, L-position ));
}
Printf ("min time: % ds, max time: % ds \ n", minTime, maxTime );
}
 
 
This article is from the "one party" blog

Related Article

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.