Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何提高效率-使用标准增量例程表示整数的字符串_C# - Fatal编程技术网

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

C# 如何提高效率-使用标准增量例程表示整数的字符串,c#,C#,我为此创建了一个线程,但后来删除了它,因为我没有说清楚 这个例程(我的代码)给出了currentCombination的字符串表示形式 using System; using System.Collections.Generic; namespace SlowGen { class MyClass { private List<char> _data = new List<char>(); private List<

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

这个例程(我的代码)给出了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;
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
名称空间慢行
{
类MyClass
{
私有列表_data=新列表();
私人名单;;
公共MyClass(列表字符,Int64 currentCombination)
{
_c=字符;
_添加数据(_c[0]);
对于(int i=0;i\u data.Count)
_添加数据(_c[0]);
其他的
{
如果(_data[charIndex]!=_c[_c.Count-1])
{
_数据[charIndex]=_c[_c.IndexOf(_data[charIndex])+1];
}
其他的
{
_数据[charIndex]=_c[0];
增量(charIndex+1);
}
}
}
public void IncrementFast()
{
递增快(0);
}
public void IncrementFast(int charIndex)
{
如果(charIndex+1>\u data.Count)
_添加数据(_c[0]);
其他的
{
如果(_data[charIndex]!=_c[_c.Count-1])
{
_数据[charIndex]=_c[_c.Count-1];
}
其他的
{
_数据[charIndex]=_c[0];
增量(charIndex+1);
}
}
}
公共字符串值
{
得到
{
字符串输出=string.Empty;
foreach(字符c在_数据中)
输出=c+输出;
返回输出;
}
}
}
}
使用此示例将创建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
List a=新列表();
a、 添加(“a”);
a、 添加(‘B’);
a、 加上(‘C’);
MyClass b=新的MyClass(a,3);
//b、 数值:C
MyClass c=新的MyClass(a,4);
//c、 价值: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;
}
static void Main(字符串[]args)
{
字符[]r=新字符[]{'A','B','C'};
对于(int i=0;i 0);
返回结果;
}
它输出A,B,C,BA,BB

我需要第一段代码的顺序和第二段代码的优雅,有人能建议吗


谢谢

您肯定已经注意到,除了“单位”列之外,还需要更改列的行为。由于您看到的“非单位”列的值太高,因此需要先减去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目标基地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
7,目标基数3为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