-1

我为此创建了一个线程,但随后删除了它,因为我没有让自己清楚。

这个例程(我的代码)给了我 currentCombination 的字符串表示。

using System;
using System.Collections.Generic;

namespace SlowGen
{
    class MyClass
    {
        private List<char> _data = new List<char>();
        private List<char> _c;

        public MyClass(List<char> chars, Int64 currentCombination)
        {
            _c = chars;
            _data.Add(_c[0]);

            for (int i = 0; i < currentCombination - 1; i++)
            {
                if (i < currentCombination - _c.Count)
                    IncrementFast();
                else
                    Increment();
            }
        }

        public void Increment()
        {
            Increment(0);
        }

        public void Increment(int charIndex)
        {
            if (charIndex + 1 > _data.Count)
                _data.Add(_c[0]);
            else
            {
                if (_data[charIndex] != _c[_c.Count - 1])
                {
                    _data[charIndex] = _c[_c.IndexOf(_data[charIndex]) + 1];
                }
                else
                {
                    _data[charIndex] = _c[0];
                    Increment(charIndex + 1);
                }
            }
        }
        public void IncrementFast()
        {
            IncrementFast(0);
        }
        public void IncrementFast(int charIndex)
        {
            if (charIndex + 1 > _data.Count)
                _data.Add(_c[0]);
            else
            {
                if (_data[charIndex] != _c[_c.Count - 1])
                {
                    _data[charIndex] = _c[_c.Count-1];
                }
                else
                {
                    _data[charIndex] = _c[0];
                    Increment(charIndex + 1);
                }
            }
        }

        public string Value
        {
            get
            {
                string output = string.Empty;
                foreach (char c in _data)
                    output = c + output;
                return output;
            }
        }
    }
}

使用此示例将创建 A、B、C、AA、AB、AC、BA 等。

List<char> a = new List<char>();
a.Add('A');
a.Add('B');
a.Add('C');
MyClass b = new MyClass(a,3);
//b.Value: C
MyClass c = new MyClass(a,4);
//c.Value: AA

现在我有了这段代码,效率更高,但模式不同

static void Main(string[] args)
{
    char[] r = new char[] { 'A', 'B', 'C' };
    for (int i = 0; i <= 120; i++)
    {
        string xx = IntToString(i, r);
        Console.WriteLine(xx);
        System.Threading.Thread.Sleep(100);
    }
    Console.ReadKey();
}

public static string IntToString(int value, char[] baseChars)
{
    string result = string.Empty;
    int targetBase = baseChars.Length;

    do
    {
        result = baseChars[value % targetBase] + result;
        value = value / targetBase;
    } 
    while (value > 0);

    return result;
}

它输出 A,B,C,BA,BB,

我需要第一部分代码的顺序和第二部分的优雅,有人可以建议吗?

谢谢

4

1 回答 1

1

正如您毫无疑问地注意到的那样,您需要更改除单位列之外的列的行为。由于您看到的非单位列的值 1 太高,您需要先减去 1 来补偿。或者至少这似乎在这里起作用:

public static string IntToString(int value, char[] baseChars)
{
    string result = string.Empty;
    int targetBase = baseChars.Length;

    do
    {
        int currentValue = value % targetBase;
        result = baseChars[currentValue] + result;
        value = value - currentValue; //possibly not necessary due to integer division rounding down anyway
        value = value / targetBase;
        value = value - 1;
    } 
    while (value > -1);

    return result;
}

以下是一些工作示例:

6 与 targetBase 2 是 AAA:

6%2 is 0, place A on right, half to 3, subtract 1 to 2
2%2 is 0, place A, half to 1, subtract 1 to 0
0%2 is 0, place A, we're done

5 与 targetBase 2 是 BB:

5%2 is 1, place B on right, subtract 1, half to 2, subtract 1 to 1
1%2 is 1, place B, subtract 1, we're done

目标基数为 3 的 7 是 BB:

7%3 is 1, place B on right, subtract 1 to 6, 1/3 to 2, subtract 1 to 1
1%3 is 1, place B on right, subtract 1, we're done
于 2013-06-06T03:44:20.960 回答