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
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
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();
how can i present in C?
i dont understand go right or left
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.
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
if i go from T to F , then 18-5 = 13. how come give different?Originally Posted by citizen
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.