[乘積數值搜尋] 輸入 1 個整數 n,0 ≤ n ≤ 9,請找尋九九乘法表的乘積數值中,
任一位數有出現該整數 n 的乘積數值,將這些乘積數值由小到大排列輸出,且輸
出之乘積數值不可重複;舉例來說,若輸入 n=6,則輸出 6 16 36 56 63 64
的乘積數值搜尋結果;注意,九九乘法表是從 2×1=2 開始,至 9×9=81 為止。
輸入/輸出說明:請在表單上建立 1 個 LABEL、2 個 TEXTBOX 及一個按鈕。LABEL
標示「輸入 n」,相對應的第 1 個 TEXTBOX 可以輸入一個整數 n (0 ≤ n ≤ 9),按
下按鈕後,在第 2 個 TEXTBOX 中,輸出所有搜尋到的乘積數值,這些乘積數值彼此
間以空格隔開,並由小到大排列。程式執行順序:輸入 n,按下按鈕,輸出。

1)使用雙層迴圈 9×9 乘法表走訪一遍。每一列起始值為 2(外層控制每一列開始)
For i = 2 To 9
    For j = 1 To i

    Next
Next

2)找出拾位數為 n 或 個位數為 n 者,該數設置旗標表示用過了,避免重複。
If (((k \ 10) = n) Or ((k Mod 10) = n)) Then
    flag(k) = True
End If

3)9×9 乘法表走訪完畢後
搜尋最小數字 2×1 至最大數字 9×9,輸出曾經被旗標設置數字
For i = 2 To 81
    If flag( i ) Then
        TextBox2.Text = TextBox2.Text & i & " "
    End If
Next



家豪版
Dim i As Integer, j As Integer, k As Integer, n As Integer = Val(TextBox1.Text)
Dim flag(81) As Boolean "vb 陣列初值為 false, 所以不用歸零
TextBox2.Text = "" "此為全域性, 必須歸零
For i = 2 To 9 "9 X 9 乘法全搜尋一遍
    For j = 1 To i
        k = i * j
        If (((k \ 10) = n) Or ((k Mod 10) = n)) Then "只要是個位數或拾位數可被 n 整除
            flag(k) = True "設置旗標
        End If
    Next
Next
For i = 2 To 81
    If flag(i) Then "將曾經被設置旗標者, 輸出
        TextBox2.Text = TextBox2.Text & i & " "
    End If
Next



昊朋版

使用 ArrayList 物件記錄 0~9 所有的數字,出現的位置

Dim list(9) As ArrayList

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox2.Text = ""
    For i = 2 To 9
        For j = 1 To 9
            Dim temp As Integer = i * j
            list(temp Mod 10).Remove(temp)
            list(temp Mod 10).Add(temp)
            If temp >= 10 Then
                list(temp \ 10).Remove(temp)
                list(temp \ 10).Add(temp)
            End If
        Next
    Next

    list(Val(TextBox1.Text)).Sort()
    For Each x In list(Val(TextBox1.Text)).ToArray
        TextBox2.Text &= x & " "
    Next
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For i = 0 To 9
        list(i) = New ArrayList
    Next
End Sub


優點:
1)任何數字都在記錄中。(但每次都只用一個數字而已)

缺點:
1)重複演算(藍色部分)。(可改善)
2)沒必要的數字可以不用記錄。(可以改善,但不是重點)
3)需排序。
4)多出一些記憶體用量。

建議改善:
1)將運算尋找及排序的部分,在 Form1_Load 做一次就好,主程式就可以重複拿來使用。

Dim list(9) As ArrayList

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim i As Integer, n As Integer = Val(TextBox1.Text)
    TextBox2.Text = list(n).Item(i)
    For i = 1 To list(n).Count - 1 "重複使用即可
        TextBox2.Text &= " , " & list(n).Item(i) "連接所以與 n 有關的數字
    Next
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim temp As Integer
    For i = 0 To 9
        list(i) = New ArrayList
    Next
    For i = 2 To 9 "搜尋 9 9 乘法表內所有數字
        For j = 1 To 9
            list(temp Mod 10).Remove(temp)
            list(temp Mod 10).Add(temp) "取個位數
            If temp >= 10 Then
                list(temp \ 10).Remove(temp)
                list(temp \ 10).Add(temp) "取拾個位數
            End If
        Next
    Next

    For i = 0 To 9 "排序每個數字
        list(i).Sort()
    Next
End Sub



黃翰版

這一版使用陣列,去搜尋,避開雙迴圈的時間延遲,是不錯的想法,但用在比賽不是很優。

1)9 9 乘法表雙迴圈也只不過 (8*9=72) 72 次執行,犯不著花時間在這做文章,萬一有個閃失,得不償失。
2)若真要用陣列處理,有兩個注意事項:
    A.陣列請用程式產生,不要一個一個鍵入資料,人工最怕疏失。
    B.陣列的產生,最好放在 Form_Load 內執行。

Dim n As Integer = val(TextBox1.Text)
Dim x() As Integer = {2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, 28, 30, 32, 35, 36, 40, 42, 45, 48, 49, 54, 56, 63, 64, 72, 81}
Dim i As Integer
Dim str As String = ""
If n < 1 Or n > 9 Then
    msgbox("n範圍在1~9,您已超過")
    Exit Sub
End If
For i = 0 To UBound(x)
    If x(i) Mod 10 = n Or x(i) \ 10 = n Then
        str &= x(i) & " "
    End If
Next
TextBox2.Text = str


改良版

Dim x() As Integer "宣告為全域變數

"主程式用法不變
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim n As Integer = Val(TextBox1.Text)
    Dim i As Integer
    Dim str As String = ""
    If n < 1 Or n > 9 Then
        MsgBox("n範圍在1~9,您已超過")
        Exit Sub
    End If
    For i = 0 To UBound(x)
        If x(i) Mod 10 = n Or x(i) \ 10 = n Then
            str &= x(i) & " "
        End If
    Next
    TextBox2.Text = str
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim i As Integer, j As Integer, k As Integer = 0
    Dim f(81) As Boolean
    For i = 2 To 8
        For j = 1 To 9
            If (f(i * j) = False) Then
                k = k + 1 "計算共有幾個
                f(i * j) = True "利用旗標記錄發生過的事
            End If
        Next
    Next
    ReDim x(k - 1) "動態陣列
    j = 0
    For i = 2 To 81
        If f(i) Then
            x(j) = i "將旗標記錄的事搬到陣列
            j = j + 1
        End If
    Next
End Sub