如何提高效率 - 使用标准增量例程的整数字符串表示

时间:2013-06-06 03:25:59

标签: c#

我为此创建了一个帖子但后来删除了它,因为我没有说清楚。

这个例程(我的代码)给了我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,

我需要第一部分代码的序列与第二部分的优雅,任何人都可以建议吗?

三江源

1 个答案:

答案 0 :(得分: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;
}

以下是一些有用的例子:

targetBase 2的

6是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
带有targetBase 2的

5是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