Dim n As Long = 1000
'p80. (遞迴定義)將一個 10 進位的數字換成 n 進位數字(2<=n<=36)。
Dim D, N As Integer
D = InputBox("D = ", "p80. (遞迴定義)將一個 10 進位的數字換成 n 進位數字(2<=n<=36)。", 1)
N = InputBox("N = ", "p80. (遞迴定義)將一個 10 進位的數字換成 n 進位數字(2<=n<=36)。", 1)
Me.TextBox1.Text = D & " = ( " & D2N(D, N) & " )" & N
'Bubble Sort
'產生 1000 data
Dim a(n - 1) As Integer
For i = 0 To UBound(a)
a(i) = Int(Rnd() * 101) '0~100
Next
'Dump a
'排序前
Me.TextBox2.Text = Dump(a)
'Bubble sort
'BubbleSort(a)
Array.Sort(a)
Me.TextBox3.Text = Dump(a)
End Sub
'擲骰子100次,計算各點出現次數
Randomize()
Dim a(99) As Integer
For i = 0 To UBound(a)
a(i) = Int(Rnd() * 6) + 1
Next
'計算各點出現次數
Dim s(6)
For Each p In a
s(p) += 1
Next
'印出結果
Dim ans = RSet("點數", 2) & " " & "次數" & vbNewLine
For i = 1 To 6
ans &= RSet(i, 2) & " " & StrDup(s(i), "#") & " " & s(i) & vbNewLine
Next
Me.TextBox4.Text = ans
'丟銅板100次,計算正反出現次數
Randomize()
Dim a(99) As Integer
For i = 0 To UBound(a)
a(i) = Int(Rnd() * 2)
Next
'計算正反出現次數
Dim s(2)
For Each p In a
s(p) += 1
Next
'印出結果
Dim ans = RSet("銅板正反", 2) & " " & "次數" & vbNewLine
For i = 0 To 1
ans &= RSet(i, 2) & " " & StrDup(s(i), "#") & " " & s(i) & vbNewLine
Next
Me.TextBox5.Text = ans
'發牌(1-13)100次,計算各數字出現次數
Randomize()
Dim a(99) As Integer
For i = 0 To UBound(a)
a(i) = Int(Rnd() * 13) + 1
Next
'計算各數字出現次數
Dim s(13)
For Each p In a
s(p) += 1
Next
'印出結果
Dim ans = RSet("數字", 2) & " " & "次數" & vbNewLine
For i = 1 To 13
ans &= RSet(i, 2) & " " & StrDup(s(i), "#") & " " & s(i) & vbNewLine
Next
Me.TextBox6.Text = ans
Module _1110534013
Dim c = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" '字串的查表
Function D2N(D, N)
If N > 36 Or N < 2 Then
Return "error"
ElseIf D < N Then
Return Mid(c, D + 1, 1)
Else
Return D2N(D \ N, N) & Mid(c, (D Mod N) + 1, 1)
End If
End Function
'swap 交換
Sub swap(ByRef x, ByRef y)
Dim t = x
x = y
y = t
End Sub
'Dump array value
Function Dump(a)
Dim ans = ""
For Each p In a
ans &= p & " "
Next
Return ans
End Function
'Bubble sort
Sub BubbleSort(a)
For i = 1 To UBound(a) 'pass 回合
For j = 0 To UBound(a) - i
If a(j) > a(j + 1) Then swap(a(j), a(j + 1))
Next
Next
沒有留言:
張貼留言