문제

I'm trying to find the distance between two characters in the alphabet. However, it's OK to go from A->Z or Z->A in one step. Given two characters, how do I find the distance?

도움이 되었습니까?

해결책

Subtract the two characters from each other making the result positive if it's negative.

From there, the answer is either that value, or if it's more than 13, it's 26 - value.

char a = '<SOME LETTER>';
char b = '<SOME LETTER>';

char distance = abs(a-b);
if (distance > 13) {
  distance = 26 - distance;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top