“如何以随机方式分割字符串” [英] "how to split a string in a random way"

查看:110
本文介绍了“如何以随机方式分割字符串”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




hello and soory for my english

这里是查询:如何以随机的方式分割字符串

我尝试在vb 2005 express中拍摄我的第一张照片,并希望以随机的方式将一个数字分成几个部分,但没有成功。

例如,如果号码是123 456:我想要一些随机的

字符串,比如theese:

(12 * 10 000)+(345 * 10)+(6 * 1)



(123 * 1 000)+(4 * 100)+(5 * 10)+(6 * 1)

等...


i试过这个:

Dim min As Long = 9999999

Dim max As Long = 999999999999

Dim Nbchi As Short = 0''多少位数?

Dim NbMorcMax As Short = 0''最多多少件?

Dim NbMorcMin As Short = 0

Dim TailleMorcMax短片= 0''长片?

Dim TailleMorcMin短片= 0

''轮胎un nombre




NbreTire = Math.Round(objRandom) .Next(min,max)+

objRandom.NextDouble(),3)

循环直到NbreTire 0

''on calcule le nombre de chiffres de lapartieentière

Nbchi = Len(Int(NbreTire))

''peupler un tableau aveclesélémentsdelapartieentière

Dim TabDecomposition(-1)As String

Dim i As Short = 0

Dim j As Short = 0

Dim q As Short = 0

q = Math.Round(objRandom.Next(1,Nbchi))

Dim PartieEntiere As Long = Int(NbreTire)

Do

TabDecomposition(j)="(" &安培; Mid $(PartieEntiere,i + 1,q)& " *

" &安培;格式(10 ^(Nbchi - q),#,#)& " )"


j + = 1

i + = q

循环直到q = Nbchi

但这不是好方法,因为在循环中q = Nbchi是非常罕见的...我需要非常幸运... b / b
所以,我想一个数字123 456的数组切成碎片所以我需要知道有多少件因此需要做多少循环


希望你明白什么我的意思是


你可以帮助

谢谢诺曼底

pascal



hello and soory for my english
here is the query :"how to split a string in a random way"
I try my first shot in vb 2005 express and would like to split a number in
several pieces in a random way without success.
for example if the number is 123 456 : i would like to have some random
strings like theese :
(12 * 10 000) + (345 * 10) + (6*1)
or
(123*1 000)+(4*100)+(5*10)+(6*1)
etc...

i tried this :
Dim min As Long = 9999999
Dim max As Long = 999999999999
Dim Nbchi As Short = 0 ''how many digit ?
Dim NbMorcMax As Short = 0 ''how many pieces max ?
Dim NbMorcMin As Short = 0
Dim TailleMorcMax As Short = 0 ''lenght of the pieces ?
Dim TailleMorcMin As Short = 0
''on tire un nombre

Do
NbreTire = Math.Round(objRandom.Next(min, max) +
objRandom.NextDouble(), 3)
Loop Until NbreTire 0
'' on calcule le nombre de chiffres de la partie entière
Nbchi = Len(Int(NbreTire))
''peupler un tableau avec les éléments de la partie entière
Dim TabDecomposition(-1) As String
Dim i As Short = 0
Dim j As Short = 0
Dim q As Short = 0
q = Math.Round(objRandom.Next(1, Nbchi))
Dim PartieEntiere As Long = Int(NbreTire)
Do
TabDecomposition(j) = "( " & Mid$(PartieEntiere, i + 1, q) & " *
" & Format(10 ^ (Nbchi - q), "#,#") & " ) "

j += 1
i += q
Loop Until q = Nbchi
but this is not the good way because in the loop q = Nbchi is very rare... i
have to be very lucky...
so, i think about an array of that number 123 456 cut into pieces so i
should know how many pieces and so how many loop i have to do

hope you understand what i mean

can you help
thank you from Normandy
pascal

推荐答案

(PartieEntiere,i + 1,q)& " *

" &安培;格式(10 ^(Nbchi - q),#,#)& " )"


j + = 1

i + = q

循环直到q = Nbchi

但这不是好方法,因为在循环中q = Nbchi是非常罕见的...我需要非常幸运... b / b
所以,我想一个数字123 456的数组切成碎片所以我需要知道有多少件因此需要做多少循环


希望你明白什么我的意思是


你可以帮助

谢谢诺曼底

pascal
(PartieEntiere, i + 1, q) & " *
" & Format(10 ^ (Nbchi - q), "#,#") & " ) "

j += 1
i += q
Loop Until q = Nbchi
but this is not the good way because in the loop q = Nbchi is very rare... i
have to be very lucky...
so, i think about an array of that number 123 456 cut into pieces so i
should know how many pieces and so how many loop i have to do

hope you understand what i mean

can you help
thank you from Normandy
pascal


嗨Pascal:


你不是很想完成它随机相反,你想要一个排列。你想要随机选择一个固定数量的排列。
。 (如果那个

是有道理的。)


我希望我正确地读到你想要的是那些因素串起来

因此我花了一些时间写这篇文章。我得到了一个很好的写作算法,所以它很有趣但是我不确定我是否会从我的算法库中拉出这个
再次使用:-)


我已经使用名为Factorize的共享方法创建了一个类实用程序。它
期望两个参数是一个数字和一个整数数组,代表

permutation。你要的那个。通过让你传入排列(而不是常规选择一个),你可以通过简单地使用

相同的排列重复序列。你的工作是生成一个简短的例程,以随机的方式生成整数数组。作为旁注你不要反复调用

随机数函数来做到这一点,你实质上只是

" shuffling"值为0到5或6.所以查找一个随机算法。


你这样称呼:


Dim order1 As Integer( )= {0,1,2,3,4,5,6}

Debug.WriteLine(Utility.Factorize(123456,order1))


Dim order2 As Integer()= {6,5,4,3,2,1,0}

Debug.WriteLine(Utility.Factorize(123456,order2))

>
Dim order3 As Integer()= {5,6,2,3,4,1,0}

Debug.WriteLine(Utility.Factorize(123456,order3))


Dim order4 As Integer()= {2,0,1}

Debug.WriteLine(Utility.Factorize(3456,order4))


这些值表示10的连续幂,因此0表示10 ^ 0或

1,10 ^ 1 = 10等。请注意,位数是可变的,在

最后一个例子它是一个小数字,所以我知道10 ^ 2 = 1000就是所有需要的b / b。虽然我可以发送{3,2,0,1}或{2,5,4,3,1,0} exta

10的幂将永远不会带回来并赢得'出现在

返回的字符串中。


希望这对你有用,你现在要做的就是生成订单

数组。它应该是一个独特的集合(例如,不要发送4次)。我认为你可以跳过甚至跳过序列中的一个数字,它应该仍然是工作的b $ b。你得到的组合是你向Factorize()方法发送

的订单的直接结果,因为它按照你指定的顺序进行数学计算。 />

如果这是课堂作业,请向老师提一下我可以使用

课程学分:-)


Tom

公共类实用程序

继承对象

公共共享函数Factorize(ByVal num As Integer,ByVal order As

Integer())As String


Dim Result As String =""


Dim res(order。长度 - 1)作为整数

order.CopyTo(res,0)


Dim div(order.Length - 1)As Integer

order.CopyTo(div,0)


Dim bal As Integer = num


For i As Integer = 0 To(order .Length - 1)

Dim val As Integer = order(i)

div(val)= CInt(10 ^ val)

res (val)=(bal \ div(val))

bal - =(div(val)* res(val))

下一页


我的整数=(order.Length - 1)到0步-1

如果(res(i)0)那么

结果+ ="(" + res(i).ToString +" * + div(i).ToString +

")" _

+ IIf(i 0," +","")。ToString

结束如果

下一页

返回结果


结束功能


结束班级


>
Pascal < sc ********* @ wanadoo.rfwrote in message

news:45 ********************* ** @ news.orange.fr ...
Hi Pascal:

You don''t quite want it done "randomly" rather you want a permutation. You
want to randomly choose one of the fixed number of permutations. (if that
makes sense.)

I hope I read it correctly that that what you want is those factored strings
as a result because I spent some time writing this. I get a kick out of
writing algorithms so it was fun but I''m not sure I''ll ever pull this one
out of my library of algorithms to use again :-)

I''ve created a Class Utility with a shared method called Factorize. It
expects two arguments a number and an array of integers that represent the
"permutation" that you want. By having you pass in the permutation (rather
than the routine choose one) you can repeat the sequence by simply using the
same permutation. Your job will be to produce a short routine to generate
the array of integers in a random way. And as a sidenote you don''t call the
random number function repeatedly to do this, you are in essence just
"shuffling" the values 0 through 5 or 6. So look up a shuffle algorithm.

You call it like this:

Dim order1 As Integer() = {0, 1, 2, 3, 4, 5, 6}
Debug.WriteLine(Utility.Factorize(123456, order1))

Dim order2 As Integer() = {6, 5, 4, 3, 2, 1, 0}
Debug.WriteLine(Utility.Factorize(123456, order2))

Dim order3 As Integer() = {5, 6, 2, 3, 4, 1, 0}
Debug.WriteLine(Utility.Factorize(123456, order3))

Dim order4 As Integer() = {2, 0, 1}
Debug.WriteLine(Utility.Factorize(3456, order4))

The values represent the successive powers of 10 so 0 represents 10 ^ 0 or
1, 10 ^ 1 = 10, etc. Note that the number of places is variable, in the
last example it is a small number so I know that 10 ^ 2 = 1000 is all that
is needed. While I could send {3, 2, 0, 1} or { 2, 5, 4, 3, 1, 0 } the exta
powers of 10 will never come back with a value and won''t appear in the
returned string.

Hopefully this works for you all you have to do now is generate the "order"
array. It should be a unique set (don''t send 4 twice for instance). I
think you can skip even skip a number in the sequence and it should still
work. The combination you get back is a direct result of the order you send
to the Factorize() method because it does the math in the order you are
specifying.

If this is a class assignment, mention me to the teacher I could use the
course credit :-)

Tom
Public Class Utility
Inherits Object

Public Shared Function Factorize(ByVal num As Integer, ByVal order As
Integer()) As String

Dim Result As String = ""

Dim res(order.Length - 1) As Integer
order.CopyTo(res, 0)

Dim div(order.Length - 1) As Integer
order.CopyTo(div, 0)

Dim bal As Integer = num

For i As Integer = 0 To (order.Length - 1)
Dim val As Integer = order(i)
div(val) = CInt(10 ^ val)
res(val) = (bal \ div(val))
bal -= (div(val) * res(val))
Next

For i As Integer = (order.Length - 1) To 0 Step -1
If (res(i) 0) Then
Result += "(" + res(i).ToString + " * " + div(i).ToString +
")" _
+ IIf(i 0, " + ", "").ToString
End If
Next

Return Result

End Function

End Class


"Pascal" <sc*********@wanadoo.rfwrote in message
news:45***********************@news.orange.fr...

>


hello and soory for my english

这里是查询:如何以随机的方式拆分字符串

我尝试在vb 2005 express中首次拍摄并希望在

几个随机的方式没有成功。

例如,如果数字是123 456:我想有一些随机的

字符串像theese:

(12 * 10 000)+(345 * 10)+(6 * 1)



(123 * 1 000)+ (4 * 100)+(5 * 10)+(6 * 1)

等...


i试过这个:

Dim min As Long = 9999999

Dim max As Long = 999999999999

Dim Nbchi As Short = 0''多少位数?

Dim NbMorcMax As Short = 0''最多多少件?

Dim NbMorcMin As Short = 0

Dim Tai lleMorcMax As Short = 0''长度的碎片?

Dim TailleMorcMin As Short = 0

''轮胎un nombre




NbreTire = Math.Round(objRandom.Next(min,max)+

objRandom.NextDouble(),3)

循环直到NbreTire 0

''on calcule le nombre de chiffres de lapartieentière

Nbchi = Len(Int(NbreTire))

' 'peupler un tableau aveclesélémentsdelapartieentière

Dim TabDecomposition(-1)As String

Dim i As Short = 0

Dim j短= 0

Dim q As Short = 0

q = Math.Round(objRandom.Next(1,Nbchi))

Dim PartieEntiere As Long = Int(NbreTire)

Do

TabDecomposition(j)="(" &安培; Mid
>

hello and soory for my english
here is the query :"how to split a string in a random way"
I try my first shot in vb 2005 express and would like to split a number in
several pieces in a random way without success.
for example if the number is 123 456 : i would like to have some random
strings like theese :
(12 * 10 000) + (345 * 10) + (6*1)
or
(123*1 000)+(4*100)+(5*10)+(6*1)
etc...

i tried this :
Dim min As Long = 9999999
Dim max As Long = 999999999999
Dim Nbchi As Short = 0 ''how many digit ?
Dim NbMorcMax As Short = 0 ''how many pieces max ?
Dim NbMorcMin As Short = 0
Dim TailleMorcMax As Short = 0 ''lenght of the pieces ?
Dim TailleMorcMin As Short = 0
''on tire un nombre

Do
NbreTire = Math.Round(objRandom.Next(min, max) +
objRandom.NextDouble(), 3)
Loop Until NbreTire 0
'' on calcule le nombre de chiffres de la partie entière
Nbchi = Len(Int(NbreTire))
''peupler un tableau avec les éléments de la partie entière
Dim TabDecomposition(-1) As String
Dim i As Short = 0
Dim j As Short = 0
Dim q As Short = 0
q = Math.Round(objRandom.Next(1, Nbchi))
Dim PartieEntiere As Long = Int(NbreTire)
Do
TabDecomposition(j) = "( " & Mid


(PartieEntiere,i + 1,q)&

* &安培;格式(10 ^(Nbchi - q),#,#)& " )"


j + = 1

i + = q

循环直到q = Nbchi


但这不是好方法,因为在循环中q = Nbchi是非常罕见的...

i必须非常幸运...

所以,我想一下这个数字123 456的数组切成碎片所以我需要知道有多少件因此需要做多少循环


希望你理解我的意思


你可以帮助

谢谢诺曼底

pascal

(PartieEntiere, i + 1, q) & "
* " & Format(10 ^ (Nbchi - q), "#,#") & " ) "

j += 1
i += q
Loop Until q = Nbchi
but this is not the good way because in the loop q = Nbchi is very rare...
i have to be very lucky...
so, i think about an array of that number 123 456 cut into pieces so i
should know how many pieces and so how many loop i have to do

hope you understand what i mean

can you help
thank you from Normandy
pascal



这篇关于“如何以随机方式分割字符串”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆