我有一个Array
chars
,除了每个字符最多只出现一次外,它是完全随机的。
我还有一个只包含数组中存在的字符的字符串。我希望此字符串“向上计数”(如果有意义),例如"111"
变为"112"
,或"aaa"
变为"aab"
。
我们假设Array
的{{1}}包含chars
。上面的示例有效,但当字符串为1, 2, and 3
(应该成为"333"
)时,我的函数返回空 - 或错误 - "1111"
。
如果我再次调用该函数,提供错误的string
,几次后它会返回正确的值(string
)。
为什么会这样?
这是我的功能:
"1111"
有什么想法吗?
编辑:
例如,我有数组Public Function getNextString(ByVal currentStr As String, ByVal pattern() As Char) As String
'currentStr is the string which I want to count upwards, pattern() is the array of chars
Dim nextStr As String = ""
Dim currentStrArray() As Char = currentStr.ToCharArray
Dim currenStrPosition As Integer = currentStrArray.Length - 1
Dim finished As Boolean = False
Do Until finished = True
Dim newPosition As Integer = getPositionInArray(currentStrArray(currentStrPosition)) 'this is a custom function, should be self-explaining
If newPosition = Nothing Then Return Nothing
newPosition += 1
Try
currentStrArray(currenStrPosition) = pattern(newPosition)
finished = True
Catch ex As IndexOutOfRangeException
currentStrArray(currentStrPosition) = pattern(0)
currentStrPosition -= 1
End Try
If currentStrPosition < 0 Then
nextStr = pattern(0)
finished = True
End If
Loop
For i As Integer = 0 To currentStrArray.Length - 1
nextStr = nextStr & currentStrArray(i)
Next
Return nextStr
End Function
。我的{"1","2","3"}
首先是string
。我想测试这些"111"
hashsums。测试完该字符串后,我需要下一个strings
,string
。然后,"112"
,"113"
,"121"
等等。当字符串到达"122"
并且字符串不是我正在寻找的字符串时,显然没有只有3个字符的字符串(所有3个字符组合都可以使用该数组)。所以我需要再次使用4个字符。这就是"333"
应该成为"333"
的原因。
希望这会有所帮助。
编辑#2:
我发现了错误。我错误地重新定义了我的数组,所以最后一个索引是空的。这使我的"1111"
看起来很奇怪。谢谢大家的工作解决方案,祝你有个美好的一天!
答案 0 :(得分:0)
如果我理解正确,你想增加特定的基数(数组的长度)。你需要的是一个映射函数,它将你的数组字符映射到它们各自的数字,然后从你的基数转换为十进制,递增,转换和重新映射。
您可以查看此链接是否有帮助吗? Quickest way to convert a base 10 number to any base in .NET?
我刚刚在c#中写了以下内容,它似乎有效。
static void Main(string[] args)
{
char[] definition = new char[] { 'a', 'b', 'c', 'd', '9', 'x', 'y', 'z', '1', '2', '3'};
string helperstring = new String(definition);
int basenumber = definition.Length;
string mynumberasstring = "333";
Console.WriteLine(mynumberasstring);
int correspondingdecimal = 0;
for (int i = 0; i < mynumberasstring.Length; i++)
{
char x = mynumberasstring[mynumberasstring.Length - i - 1];
int index = helperstring.IndexOf(x);
int magnitude = 1;
for (int j = 0; j < i; j++)
magnitude *= basenumber;
Console.WriteLine(x + " -> " + index);
correspondingdecimal += magnitude * index;
}
Console.WriteLine(correspondingdecimal + " -> " + ++correspondingdecimal);
List<int> indicesofnewnumber = new List<int>();
int newmagnitude = basenumber;
while(correspondingdecimal > 0)
{
int div = correspondingdecimal / basenumber;
int remainder = correspondingdecimal % basenumber;
Console.WriteLine("{0} -> {1} ; {2}", correspondingdecimal, div, remainder);
indicesofnewnumber.Add(remainder);
correspondingdecimal = div;
}
string newnumberasstring = "";
for (int i = 0; i < indicesofnewnumber.Count; i++)
newnumberasstring += definition[indicesofnewnumber[indicesofnewnumber.Count - 1 - i]];
Console.WriteLine(newnumberasstring);
Console.ReadLine();
}
答案 1 :(得分:0)
好吧,我会转换为数字,加1,然后转换回字符串。
将字符串转换为数字:
Function convertNumberToString(number As Integer, pattern As Char()) As String
Dim charDigits = pattern.ToList()
Dim numberBase = charDigits.Count
Dim buffer = New Stack(Of Char)()
'var j = buffer.Length;
While number > 0
buffer.Push(charDigits(number Mod numberBase))
number = number / numberBase
End While
Dim sb = New StringBuilder()
While buffer.Count > 0
sb.Append(buffer.Pop())
End While
Return sb.ToString()
End Function
将数字转换回字符串:
Function getNextString(ByVal currentStr As String, ByVal pattern() As Char) As String
Dim number = ConvertStringToNumber(currentStr, pattern)
number += 1
Return convertNumberToString(number, pattern)
End Function
既然你有转换功能,你所要做的就是这样:
{{1}}
答案 2 :(得分:0)
这是我能想到的将数字转换为任意数字的任意基数的最小代码:
Dim convert As Func(Of Integer, Char(), String) = Nothing
convert = Function (n, cs) _
If(n \ cs.Length = 0, "", convert(n \ cs.Length, cs)) + cs(n Mod cs.Length)
因此,要将4
转换为二进制,我可以convert(4, { "0"c, "1"c })
,我会得到100
。
如果我使用String.Join(", ", Enumerable.Range(0, 32).Select(Function (n) convert(n, { "0"c, "1"c })))
查看前32个(从零开始)二进制数,那么这是一个完整性检查,我得到了这个:
0,1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111,10000,10001,10010,110011,10100,10101,10110,10111 ,11000,11001,11010,11011,11100,11101,11110,11111
或者从{0}开始从0开始的前102个十进制数字我得到了这个:
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 ,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48 ,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73 ,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98 ,99,100,101
所以我现在可以使用相同类型的代码,使用String.Join(", ", Enumerable.Range(0, 102).Select(Function (n) convert(n, "0123456789".ToCharArray())))
从数字{ "1"c, "2"c, "3"c }
获取前50个数字,我得到了这个:
1,2,3,21,22,23,31,32,33,211,212,213,221,222,223,231,232,233,311,312,313,321,322,323 ,331,332,333,2111,2112,2113,2121,2122,2123,2131,2132,2133,2211,2212,2213,2221,2222,2223,2231,2232,2233,2311,2312,2313,2321 ,2322
这是正确的&#34;计数&#34;使用数字String.Join(", ", Enumerable.Range(0, 50).Select(Function (n) convert(n, "123".ToCharArray())))
。所以,在这个编号系统中说{ "1"c, "2"c, "3"c }
是正确的。这与在我们的十进制系统中说"333" + 1 = "2111"
相同 - 它显然不会来自999 + 1 = 1000
,这意味着999 + 1 = 0000
在这个&#34; 123&#中的含义34;系统。严格来说,我应该写"333" + 1 = "1111"
(因为"333" + "2" = "2111"
代表"2"
)。
如果您想要所有组合,那么您需要使用&#34;零&#34;将字符串填充到左侧。字符 - 在这种情况下1
。然后这段代码:
1
...给出以下81个数字:
1111,1112,1113,1121,1122,1123,1131,1132,1133,1211,1212,1213,1221,1222,1223,1231,1232,1233,1311,1312,1313,1321,1322,1323 ,1331,1332,1333,2111,2112,2113,2121,2122,2123,2131,2132,2133,2211,2212,2213,2221,2222,2223,2231,2232,2233,2311,2312,2313,2321 ,2322,2323,2331,2332,2333,3111,3112,3113,3121,3122,3123,3131,3132,3123,3211,3212,3213,3221,3222,3223,3231,3232,3233,3311,3312 ,3313,3321,3332,3323,3331,3332,3333
...和81是String.Join(", ", _
Enumerable _
.Range(0, 500) _
.Select(Function (n) convert(n, "123".ToCharArray()).PadLeft(4, "1"c)) _
.Where(Function (x) x.Length = 4))
的组合总数。
以下是两种转换方式的功能:
3 x 3 x 3 x 3 = 81