递增字母表
我正在尝试创建一个函数,当传递索引时,该函数将给出字母表位置。 这与 Excel 显示其列的方式相同。 A...Z、AA、AB...我编写了下面的函数来获取 Z 之前的结果。看起来
static string GetColumnName(int index)
{
const int alphabetsCount = 26;
if (index <= alphabetsCount)
{
int code = (index - 1) + (int)'A';
return char.ConvertFromUtf32(code);
}
return string.Empty;
}
这在“Z”之前都可以正常工作。 如果我通过 1,它返回“A”;如果我通过 2,它返回“B”,依此类推。 但是,我无法弄清楚当我将 27 传递给这个函数时如何获得 AA。 我想我需要一种递归方法来找到它。
对这个问题的任何投入都会很棒!
编辑
这是 Tordek 建议的。 但他的代码会在 52、78 等数字中失败。为此添加了解决方法,这是最终的工作代码。
static string GetColumnName(int index)
{
const int alphabetsCount = 26;
if (index > alphabetsCount)
{
int mod = index % alphabetsCount;
int columnIndex = index / alphabetsCount;
// if mod is 0 (clearly divisible) we reached end of one combination. Something like AZ
if (mod == 0)
{
// reducing column index as index / alphabetsCount will give the next value and we will miss one column.
columnIndex -= 1;
// passing 0 to the function will return character '@' which is invalid
// mod should be the alphabets count. So it takes the last char in the alphabet.
mod = alphabetsCount;
}
return GetColumnName(columnIndex) + GetColumnName(mod);
}
else
{
int code = (index - 1) + (int)'A';
return char.ConvertFromUtf32(code);
}
}
I am trying to create a function which will give me alphabet position when an index is passed. It will be same like how excel shows it's columns. A...Z, AA,AB.... I wrote the below function to get the results upto Z. It looks like
static string GetColumnName(int index)
{
const int alphabetsCount = 26;
if (index <= alphabetsCount)
{
int code = (index - 1) + (int)'A';
return char.ConvertFromUtf32(code);
}
return string.Empty;
}
This works fine until 'Z'. It return 'A' if I pass 1 and return 'B' if I pass 2 and so on. But, I am not able to figure out how will I get AA when I pass 27 to this function. I guess I need a recursive method to find it.
Any inputs to this problem will be great!
Edit
This is suggested by Tordek. But his code will fail in numbers like 52, 78 etc. Added workaround for that and here is the final working code.
static string GetColumnName(int index)
{
const int alphabetsCount = 26;
if (index > alphabetsCount)
{
int mod = index % alphabetsCount;
int columnIndex = index / alphabetsCount;
// if mod is 0 (clearly divisible) we reached end of one combination. Something like AZ
if (mod == 0)
{
// reducing column index as index / alphabetsCount will give the next value and we will miss one column.
columnIndex -= 1;
// passing 0 to the function will return character '@' which is invalid
// mod should be the alphabets count. So it takes the last char in the alphabet.
mod = alphabetsCount;
}
return GetColumnName(columnIndex) + GetColumnName(mod);
}
else
{
int code = (index - 1) + (int)'A';
return char.ConvertFromUtf32(code);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
任何递归函数都可以转换为等效的迭代函数。 我发现首先递归地思考总是很容易:
可以简单地转换为:
即便如此,听听乔尔的话。
Any recursive function can be converted into an equivalent iterative one. I find it always easy to think recursively first:
Which can be simple converted into:
Even so, listen to Joel.
请参阅这个问题:
将列索引转换为 Excel 列名称
或此名称:
如何将列号(例如 127)转换为 Excel 列(例如 AA)
虽然第一个链接在顶部有一个正确的答案,而第二个链接有几个不正确的答案。
See this question:
Translate a column index into an Excel Column Name
or this one:
How to convert a column number (eg. 127) into an excel column (eg. AA)
Though the first link has a correct answer right at the top and the 2nd has several that are not correct.
递归是一种可能性——如果
index > 26
,您在此调用中处理index % 26
并将其连接到index / 26
上的递归调用。 然而,迭代通常更快,并且对于像这样的简单情况来说并不难安排。 用伪代码:等。
Recursion is one possibility -- if
index > 26
, you deal withindex % 26
in this call and concatenate it to a recursive call onindex / 26
. However, iteration is often speedier and not hard to arrange for simple cases such as this one. In pseudocode:or the like.
我的 C# 非常糟糕且生锈。 将其解释为伪代码 - 它几乎肯定不会编译,但可能会帮助您入门。
My C# is HORRIBLE AND RUSTY. Interpret this as pseudocode - it will almost certainly not compile, but may get you started.