2013-06-06 19 views
-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,

我需要的代碼的第一部分的序列與第二的優雅,C有人建議?

三江源

回答

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 

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