递增字母表

发布于 2024-07-22 05:03:51 字数 1517 浏览 5 评论 0原文

我正在尝试创建一个函数,当传递索引时,该函数将给出字母表位置。 这与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我们的影子 2024-07-29 05:03:51

任何递归函数都可以转换为等效的迭代函数。 我发现首先递归地思考总是很容易:

static string GetColumnName(int index)
{
    const int alphabetsCount = 26;

    if (index > alphabetsCount) {
        return GetColumnName(index / alphabetsCount) + GetColumnName(index % alphabetsCount);
    } else {
        int code = (index - 1) + (int)'A';
        return char.ConvertFromUtf32(code);
    }
}

可以简单地转换为:

static string GetColumnName(int index)
{
    const int alphabetsCount = 26;
    string result = string.Empty;

    while (index > 0) {
        result = char.ConvertFromUtf32(64 + (index % alphabetsCount)) + result;
        index /= alphabetsCount;
    }

    return result;
}

即便如此,听听乔尔的话。

Any recursive function can be converted into an equivalent iterative one. I find it always easy to think recursively first:

static string GetColumnName(int index)
{
    const int alphabetsCount = 26;

    if (index > alphabetsCount) {
        return GetColumnName(index / alphabetsCount) + GetColumnName(index % alphabetsCount);
    } else {
        int code = (index - 1) + (int)'A';
        return char.ConvertFromUtf32(code);
    }
}

Which can be simple converted into:

static string GetColumnName(int index)
{
    const int alphabetsCount = 26;
    string result = string.Empty;

    while (index > 0) {
        result = char.ConvertFromUtf32(64 + (index % alphabetsCount)) + result;
        index /= alphabetsCount;
    }

    return result;
}

Even so, listen to Joel.

少钕鈤記 2024-07-29 05:03:51

请参阅这个问题:
将列索引转换为 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.

一页 2024-07-29 05:03:51

递归是一种可能性——如果 index > 26,您在此调用中处理 index % 26 并将其连接到 index / 26 上的递归调用。 然而,迭代通常更快,并且对于像这样的简单情况来说并不难安排。 用伪代码:

string result = <convert `index % 26`>
while index > 26:
  index = index / 26
  result = <convert `index % 26`> + result
return result

等。

Recursion is one possibility -- if index > 26, you deal with index % 26 in this call and concatenate it to a recursive call on index / 26. However, iteration is often speedier and not hard to arrange for simple cases such as this one. In pseudocode:

string result = <convert `index % 26`>
while index > 26:
  index = index / 26
  result = <convert `index % 26`> + result
return result

or the like.

手长情犹 2024-07-29 05:03:51
static string GetColumnName(int index)
{
    const int alphabetsCount = 26;
    string result = '';

    if (index >= alphabetsCount)
    {
        result += GetColumnName(index-alphabetsCount)
    }
    return (string) (64 + index);
}

我的 C# 非常糟糕且生锈。 将其解释为伪代码 - 它几乎肯定不会编译,但可能会帮助您入门。

static string GetColumnName(int index)
{
    const int alphabetsCount = 26;
    string result = '';

    if (index >= alphabetsCount)
    {
        result += GetColumnName(index-alphabetsCount)
    }
    return (string) (64 + index);
}

My C# is HORRIBLE AND RUSTY. Interpret this as pseudocode - it will almost certainly not compile, but may get you started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文