我正在尝试编写一个带有修订号(int)的函数,并将其转换为修订名称(字符串).该公式应产生类似于此的输出:
Number Name 1 A 2 B 3 C ... ... 25 Y 26 Z 27 AA 28 AB 29 AC ... ... 51 AY 52 AZ 53 BA 54 BB 55 BC ... ...
这看起来很简单,但我认为它可能涉及递归,我很可怕.有什么建议?
我认为这与从列号中计算Excel列名称相同:
private string GetExcelColumnName(int columnNumber)
{
int dividend = columnNumber;
string columnName = String.Empty;
int modulo;
while (dividend > 0)
{
modulo = (dividend - 1) % 26;
columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
dividend = (int)((dividend - modulo) / 26);
}
return columnName;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我认为你基本上需要将10x数值系统中的数字转换为26x数值系统中的数字.
例如:
53 = 5*10 ^ 1 + 3*10 ^ 0 = [5] [3]
53 = B*26 ^ 1 + A*26 ^ 0 = [B] [A]
int value10 = 53;
int base10 = 10;
string value26 = "";
int base26 = 26;
int input = value10;
while (true)
{
int mod = input / base26;
if (mod > 0)
value26 += Map26SymbolByValue10 (mod); // Will map 2 to 'B'
else
value26 += Map26SymbolByValue10 (input); // Will map 1 to 'A'
int rest = input - mod * base26;
if (input < base26) break;
input = rest;
}
Run Code Online (Sandbox Code Playgroud)