2012年1月2日 星期一

1000 到 9999 不重複的數

從 1000 到 9999 中找出數字不能重複的數,並將符合條件者全部印出。


例如: 1234

6 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #1
    Dim ans(4) As String

    For i = 1000 To 9999
    For j = 1 To 4
    ans(j) = Mid(i, j, 1)
    Next
    If ans(1) <> ans(2) And ans(2) <> ans(3) And ans(3) <> ans(1) And ans(2) <> ans(4) And ans(3) <> ans(4) And ans(1) <> ans(4) Then
    Print #1, ans(1) & ans(2) & ans(3) & ans(4)
    End If
    Next

    Close
    End
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Open App.Path & "\out.txt" For Output As #1
    Dim x(4) As String
    For i = 1000 To 9999
    For j = 1 To 4
    x(j) = Mid(i, j, 1)
    Next j
    If x(1) <> x(2) And x(1) <> x(3) And x(1) <> x(4) And x(2) <> x(3) And x(2) <> x(4) And x(3) <> x(4) Then
    Print #1, x(1) & x(2) & x(3) & x(4)
    End If
    Next i
    Close
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    me.hide
    Open App.Path & "\out.txt" For Output As #1

    For x = 1000 To 9999
    P = False

    For i = 1 To 4 - 1
    For j = i + 1 To 4
    If Mid(x, i, 1) = Mid(x, j, 1) Then P = True
    Next j
    Next i

    If P = False Then Print #1,X
    Next x

    close
    end
    End Sub

    回覆刪除
  4. 小冰,哲,柯佑好,
    三個程式都正確。
    訓練思緒是否完整的題目。

    副題:
    將所有不重複數字的數,輸出到檔案1,將這些數字的個數n,和最後一個數(最大的一個數)x, 將n 和 x 輸出到檔案2。

    回覆刪除
  5. 副題:

    Private Sub Form_Load()
    Open App.Path & "\out.txt" For Output As #1
    Open App.Path & "\out2.txt" For Output As #2
    Dim t As Boolean
    n = 0
    For i = 100 To 9999
    t = False
    For j = 1 To Len(i) - 1 '這樣要找比1000小的就直接改for i 初值就好
    For k = j + 1 To Len(i)
    If Mid(i, j, 1) = Mid(i, k, 1) Then t = True
    Next k
    Next j
    If t = False Then n = n + 1: Print #1, i: x = i
    Next i
    Print #2, "n:" & n & "x:" & x
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  6. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #1
    Open App.Path & "\out1.txt" For Output As #2
    Dim ans(4) As String
    Dim a As Boolean
    n = 0
    For i = 1000 To 9999
    a = False
    For j = 1 To 4
    For j1 = j + 1 To 4
    If Mid(i, j, 1) = Mid(i, j1, 1) Then a = True
    Next
    Next
    If a = False Then Print #1, i: n = n + 1: c = i
    Next
    Print #2, "n:" & n & "x:" & c
    Close
    End
    End Sub

    回覆刪除