Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图找出字母表中两个字符之间的距离。但是,从 A->Z 或 Z->A 一步即可。给定两个字符,我如何找到距离?
如果结果为负,则将两个字符相减,结果为正。
从那里,答案是那个值,或者如果它超过 13,它是 26 - 值。
char a = '<SOME LETTER>'; char b = '<SOME LETTER>'; char distance = abs(a-b); if (distance > 13) { distance = 26 - distance; }