Thread: ABC problems

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    12

    ABC problems

    question:

    A to Z is a circle, if i start from T ,then want go to F, i want to choose a shorest part. how can i do it .
    T>U>V>W>X>W>Z>A>B>C>D>E>F = 12
    T>S>R>Q>P>O>N>M>L>K>J>I>H>G>F =14

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Find the difference. If the answer is a positive number, go left; if it is negative, go right. Notice that at the middle of the alphabet the distance is the same in either direction.

    Code:
    int path = target letter - start position;
    if ( path != abs(path) )
       go_right();
    else 
       go_left();

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    how can i present in C?
    i dont understand go right or left

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Go right. A -> B
    Go left. A <- Z

    In otherwords, right is counter++, left is counter--, if you're simply walking through your alphabet. Type all the letters in a line. A is on your left. Z is on your right.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm not going to give you C code while trying to explain an algorithm.

    Unfold your circle for a minute in your head. Imagine the series A to Z like so
    [A, B, C, D, E, ... Z]
    For any given letter, say T, going to letter F, you have two possible directions right or left. By finding the difference between the characters themselves, you can determine which direction would be the shortest path.

    If F is at position 5, and T is at position 18
    5 - 18 = -13, so if you went to the right direction you would be there after 12 steps.
    But notice I also said that in the middle of the alphabet the distance is the same.

    From A to M is 13 steps.
    From B to N is 13 steps.
    From C to O is 13 steps.
    .
    .
    .
    See the pattern?
    Last edited by whiteflags; 10-01-2006 at 01:57 AM. Reason: i am not a morning person

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    Quote Originally Posted by citizen
    I'm not going to give you C code while trying to explain an algorithm.

    Unfold your circle for a minute in your head. Imagine the series A to Z like so
    [A, B, C, D, E, ... Z]
    For any given letter, say T, going to letter F, you have two possible directions right or left. By finding the difference between the characters themselves, you can determine which direction would be the shortest path.

    If F is at position 5, and T is at position 18
    5 - 18 = -13, so if you went to the right direction you would be there after 12 steps.
    But notice I also said that in the middle of the alphabet the distance is the same.

    From A to M is 13 steps.
    From B to N is 13 steps.
    From C to O is 13 steps.
    .
    .
    .
    See the pattern?
    if i go from T to F , then 18-5 = 13. how come give different?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's quite simple, if the answer is >13, you should have gone the other way.
    There are only two answers, and it costs the same to work out either one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. fscanf search file HELP!
    By datoneperson in forum C Programming
    Replies: 5
    Last Post: 09-05-2008, 07:27 PM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  4. Weird memory problems (MS-VC++)
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2006, 08:01 AM
  5. Problems with exceptions
    By maneesh in forum C++ Programming
    Replies: 3
    Last Post: 11-23-2005, 05:16 PM