1

事前に感謝します。A から Z までのシーケンスを生成し、その後 0 から 9 まで、その後 AA、AB、AC ..... AZ、A0、A1 .... A9、BA などに移動します。の上

私は次のように実装しようとしました

public static string GenerateSequence(List<string> inputList)
    {
        string identifierCode = "A";
        //check if list does not contains any element
        if (!inputList.Any()) return identifierCode;
        //sort codes
        inputList.Sort();
        //get last code
        var lastItem = inputList[inputList.Count - 1];
        //split code
        var splittedChars = lastItem.ToCharArray();
        bool incrementNext = true;
        for (int i = 0; i < splittedChars.Length; i++)
        {
            if (incrementNext)
            {
                var effectedNumber = splittedChars.Length - (i + 1);
                if (effectedNumber >= 0)
                {
                    var charToIncrement = splittedChars[effectedNumber];
                    switch (charToIncrement)
                    {
                        case 'Z':
                            charToIncrement = '0';
                            incrementNext = false;
                            break;
                        case '9':
                            charToIncrement = 'A';
                            incrementNext = true;
                            splittedChars[effectedNumber] = charToIncrement;
                            break;
                        default:
                            charToIncrement++;
                            incrementNext = false;
                            break;
                    }
                    splittedChars[effectedNumber] = charToIncrement;
                }
                else
                {
                    return "A" + splittedChars;
                }
            }
        }

        return new string(splittedChars);
    }

しかし、inputList.Sort() はアルファベットの前に数字をソートするため、私のコードは Z の後に失敗します

4

3 に答える 3

2

擬似コード:

Base enumeration: 
   yield return A, B, C .... 8, 9;

Next enumeration:
   for each item in base enumeration
       for each item2 in base enumeration
           yield return item + item2

Enumeration N:
    for each item in base enumeration
       for each item2 in N-1 enumeration
           yield return item + item2

では、どうすればよいのでしょうか。これは、再帰関数の標準的な例です。

  1. 簡単に識別できる基本ケースがあります: 基本列挙です。
  2. N の深い列挙は、N マイナス 1 の深い列挙に基づいて構築されます。

それを念頭に置いて、次のコードを検討してください。

public static IEnumerable<string> GetNthEnumeration(IEnumerable<string> baseEnumeration, int n)
{
    if (baseEnumeration == null) throw new ArgumentNullException();

    if (n < 0) throw new ArgumentOutOfRangeException();

    if (n == 0) //base case
    {
        foreach (var item in baseEnumeration) { yield return item; }
    }
    else //build recursively
    {
        foreach (var pre in baseEnumeration) 
        {
            foreach (var post in GetNthEnumeration(baseEnumeration, n - 1))
            {
                yield return pre + post; 
            }
        }
    }
 }
于 2016-12-10T18:07:40.280 に答える
1

「a」が「A」よりも大きく、「A」が「0」よりも大きいという次の問題があります。次のASCIIテーブルを参照してください。ただし、独自のコンパレータIComparerを使用できます

さらに、次の方法をテストできます。

    public static string GetExcelColumnName(int index)
    {
        var alphabet = new char[]
        {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
            'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
        };
        var name = new char[3];
        var rem = index;

        for (var i = 2; i >= 0; i--)
        {
            var tmp = rem % alphabet.Length;
            var r = alphabet[tmp];
            name[i] = r;
            rem = (rem-tmp) / alphabet.Length;
        }

        return new string(name);
    }
于 2016-12-10T18:09:55.790 に答える