A,B,C,AA,AB,AC,BA,BB,BC,…みたいな文字列の作成 への返答
投稿で使用できる特殊コードの説明。(別タブで開きます。)
以下の返答は逆順(新しい順)に並んでいます。
投稿者 shu  (社会人)
投稿日時
2011/6/3 15:58:33
> intCharNum = UBound(chars)
intCharNum = UBound(chars) + 1
でどうでしょう?
intCharNum = UBound(chars) + 1
でどうでしょう?
投稿者 aex  (社会人)
投稿日時
2011/6/3 15:55:32
shuさん、ありがとうございます。
じつはVBでも、VB6なので以下のようにちょっと手直しをして動かしてみましたが、
ちょっと思いとはずれてしまってるような・・・ ("C"が全然出てこないので)
修正ミスですかねぇ???
この例だと桁数は3桁の予定です。
' Dim chars = {"", "A", "B", "C"}
chars = Array("", "A", "B", "C")
' Dim intKeta = 5
Dim intKeta As Integer
intKeta = 5
' Dim intIdxs(intKeta - 1) As Integer
Dim intIdxs(4) As Integer
' Dim intCharNum = chars.Length
Dim intCharNum As Integer
intCharNum = UBound(chars)
' Dim strText = ""
Dim strText As String
strText = ""
Dim strOutStr As String
' Array.Clear(intIdxs, 0, intKeta)
Do While intIdxs(intKeta - 1) < intCharNum
intIdxs(0) = intIdxs(0) + 1
For i = 0 To intKeta - 1
If intIdxs(i) = intCharNum Then
If i = intKeta - 1 Then
Exit Do
Else
intIdxs(i) = 1
intIdxs(i + 1) = intIdxs(i + 1) + 1
End If
Else
Exit For
End If
Next
' Dim strOutStr = ""
strOutStr = ""
For i = 0 To intKeta - 1
strOutStr = strOutStr & chars(intIdxs(i))
Next
' Console.WriteLine (strOutStr)
Debug.Print (strOutStr)
Loop
結果
-----------------
A
B
AA
BA
AB
BB
AAA
BAA
ABA
BBA
AAB
BAB
ABB
BBB
AAAA
BAAA
ABAA
BBAA
AABA
BABA
ABBA
BBBA
AAAB
BAAB
ABAB
BBAB
AABB
BABB
ABBB
BBBB
AAAAA
BAAAA
ABAAA
BBAAA
AABAA
BABAA
ABBAA
BBBAA
AAABA
BAABA
ABABA
BBABA
AABBA
BABBA
ABBBA
BBBBA
AAAAB
BAAAB
ABAAB
BBAAB
AABAB
BABAB
ABBAB
BBBAB
AAABB
BAABB
ABABB
BBABB
AABBB
BABBB
ABBBB
BBBBB
じつはVBでも、VB6なので以下のようにちょっと手直しをして動かしてみましたが、
ちょっと思いとはずれてしまってるような・・・ ("C"が全然出てこないので)
修正ミスですかねぇ???
この例だと桁数は3桁の予定です。
' Dim chars = {"", "A", "B", "C"}
chars = Array("", "A", "B", "C")
' Dim intKeta = 5
Dim intKeta As Integer
intKeta = 5
' Dim intIdxs(intKeta - 1) As Integer
Dim intIdxs(4) As Integer
' Dim intCharNum = chars.Length
Dim intCharNum As Integer
intCharNum = UBound(chars)
' Dim strText = ""
Dim strText As String
strText = ""
Dim strOutStr As String
' Array.Clear(intIdxs, 0, intKeta)
Do While intIdxs(intKeta - 1) < intCharNum
intIdxs(0) = intIdxs(0) + 1
For i = 0 To intKeta - 1
If intIdxs(i) = intCharNum Then
If i = intKeta - 1 Then
Exit Do
Else
intIdxs(i) = 1
intIdxs(i + 1) = intIdxs(i + 1) + 1
End If
Else
Exit For
End If
Next
' Dim strOutStr = ""
strOutStr = ""
For i = 0 To intKeta - 1
strOutStr = strOutStr & chars(intIdxs(i))
Next
' Console.WriteLine (strOutStr)
Debug.Print (strOutStr)
Loop
結果
-----------------
A
B
AA
BA
AB
BB
AAA
BAA
ABA
BBA
AAB
BAB
ABB
BBB
AAAA
BAAA
ABAA
BBAA
AABA
BABA
ABBA
BBBA
AAAB
BAAB
ABAB
BBAB
AABB
BABB
ABBB
BBBB
AAAAA
BAAAA
ABAAA
BBAAA
AABAA
BABAA
ABBAA
BBBAA
AAABA
BAABA
ABABA
BBABA
AABBA
BABBA
ABBBA
BBBBA
AAAAB
BAAAB
ABAAB
BBAAB
AABAB
BABAB
ABBAB
BBBAB
AAABB
BAABB
ABABB
BBABB
AABBB
BABBB
ABBBB
BBBBB
投稿者 shu  (社会人)
投稿日時
2011/6/3 15:35:08
こんなんでどう?
Dim chars = {"", "A", "B", "C"}
Dim intKeta = 5
Dim intIdxs(intKeta - 1) As Integer
Dim intCharNum = chars.Length
Dim strText = ""
Array.Clear(intIdxs, 0, intKeta)
Do While intIdxs(intKeta - 1) < intCharNum
intIdxs(0) += 1
For i = 0 To intKeta - 1
If intIdxs(i) = intCharNum Then
If i = intKeta - 1 Then
Exit Do
Else
intIdxs(i) = 1
intIdxs(i + 1) += 1
End If
Else
Exit For
End If
Next
Dim strOutStr = ""
For i = 0 To intKeta - 1
strOutStr &= chars(intIdxs(i))
Next
Console.WriteLine(strOutStr)
Loop
Dim chars = {"", "A", "B", "C"}
Dim intKeta = 5
Dim intIdxs(intKeta - 1) As Integer
Dim intCharNum = chars.Length
Dim strText = ""
Array.Clear(intIdxs, 0, intKeta)
Do While intIdxs(intKeta - 1) < intCharNum
intIdxs(0) += 1
For i = 0 To intKeta - 1
If intIdxs(i) = intCharNum Then
If i = intKeta - 1 Then
Exit Do
Else
intIdxs(i) = 1
intIdxs(i + 1) += 1
End If
Else
Exit For
End If
Next
Dim strOutStr = ""
For i = 0 To intKeta - 1
strOutStr &= chars(intIdxs(i))
Next
Console.WriteLine(strOutStr)
Loop
投稿者 aex  (社会人)
投稿日時
2011/6/3 13:15:17
VBで文字列の桁数を指定し、1桁~指定桁までの文字列を作成したいのですが、どのようなプログラムを書いたらよいかわかりません。どなたか教えてください。
配列で"A","B","C"を宣言(文字の組み合わせは自由にしたいので)
文字桁3桁を指定する
結果として
"A"
"B"
"C"
"AA"
"AB"
"AC"
"BA"
"BB"
"BC"
"CA"
"CB"
"CC"
"AAA"
"AAB"
"AAC"
"ABA"
"ABB"
"ABC"
"ACA"
"ACB"
"ACC"
"BAA"
"BAB"
"BAC"
"BBA"
"BBB"
"BBC"
"BCA"
"BCB"
"BCC"
"CAA"
"CAB"
"CAC"
"CBA"
"CBB"
"CBC"
"CCA"
"CCB"
"CCC"
順番は気にしません。それぞでの文字列が結果として得られればよいのですが・・・
配列で"A","B","C"を宣言(文字の組み合わせは自由にしたいので)
文字桁3桁を指定する
結果として
"A"
"B"
"C"
"AA"
"AB"
"AC"
"BA"
"BB"
"BC"
"CA"
"CB"
"CC"
"AAA"
"AAB"
"AAC"
"ABA"
"ABB"
"ABC"
"ACA"
"ACB"
"ACC"
"BAA"
"BAB"
"BAC"
"BBA"
"BBB"
"BBC"
"BCA"
"BCB"
"BCC"
"CAA"
"CAB"
"CAC"
"CBA"
"CBB"
"CBC"
"CCA"
"CCB"
"CCC"
順番は気にしません。それぞでの文字列が結果として得られればよいのですが・・・
ありがとうございました。