A = 0
B = 1
Z = 25
AA = 26
AB = 27
AAA
实现StrToInt
#include <assert.h>
#include <iostream>
int MyStrToInt(const char* str)
{
auto length = strlen(str);
if (length == 1)
{
return str[0] - 'A';
}
int result = str[length - 1] - 'A';
auto powValue = 26;
for (int i = length - 2; i >= 0; --i) {
result += (str[i] - 'A' + 1) * powValue;
powValue *= 26;
}
return result;
}
int main()
{
assert(MyStrToInt("A") == 0);
assert(MyStrToInt("Z") == 25);
assert(MyStrToInt("AA") == 26 + 0);
assert(MyStrToInt("AZ") == 26 + 25);
assert(MyStrToInt("ZZ") == 26 + 26 * 25 + 25);
assert(MyStrToInt("AAA") == 26 + 26 * 25 + 25 + 1);
assert(MyStrToInt("AAA") == 26 + 26 * 25 + 26);
assert(MyStrToInt("AAA") == 26 + 26 * 26);
assert(MyStrToInt("AAZ") == 26 + 26 * 26 + 25);
assert(MyStrToInt("ZZZ") + 1 == MyStrToInt("AAAA"));
return 0;
}
可能太想去了,导致最后效果很差吧