2012年4月2日 星期一

VB101小考 第1次

VB101小考 第1次


1.從檔案(in1.txt)裡讀出所有的數字(不定個數),降冪排序,輸出到檔案(out1.txt)。

2.山寨版的費氏函數。
  S(1)=10
  S(2)=-3
  S(n)=S(n-1) - S(n-2),當n>=3。
  從檔案(in2.txt)裡讀出單一個n,將S(n)的值,輸出到檔案(out2.txt)。

3.請找出某4位數的值,等於每個數字的立方和的數。全部輸出到檔案(out3.txt),若無,請輸出0000。

4.請隨機將撲克牌的52張牌,分給4個玩家。輸出到檔案(out4.txt),每次執行的結果需要不同。
 撲克牌的表示法,數字大小為(A,2,3,4,5,6,7,8,9,10,J,Q,K),花色的表示法為(黑桃,紅心,紅鑽,黑花)。
  輸出的檔案有4行,每行13張牌,不用排序。例:紅心J  黑花K  紅心2  紅鑽3 …


另說明,小考不附輸入檔(in1.txt等),請自行建立。

8 則留言:

  1. 第一題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out1.txt" For Output As #2
    Dim x(99) As Integer
    n = 0
    Do Until EOF(1)
    n = n + 1
    Input #1, x(n)
    Loop
    For i = 1 To n
    If x(i) < x(i + i) Then
    Next i
    Close #2
    Close #1
    End
    End Sub
    第二題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in2.txt" For Input As #1
    Open App.Path & "\out2.txt" For Output As #2
    Dim x(100) As Integer
    Input #1, n
    For i = 1 To n
    x(1) = 10
    x(2) = -3
    If i >= 3 Then
    x(i) = x(i - 1) - x(i - 2)
    End If
    Next i
    ans = x(n)
    Print #2, ans
    Close #2
    Close #1
    End
    End Sub
    第三題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out3.txt" For Output As #1
    Dim who As Boolean
    Dim x(10000) As Integer
    who = False
    For i = 1000 To 9999
    For j = 1 To 4
    x(j) = Val(Mid(i, j, 1))
    If x(j) ^ 3 + x(j + 1) ^ 3 + x(j + 2) ^ 3 + x(j + 3) ^ 3 = i Then
    who = True
    Print #1, i
    End If
    Next j
    Next i
    If who = False Then Print #1, "0000"
    Close #1
    End
    End Sub
    第四題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out4.txt" For Output As #1
    Dim now As Boolean
    Dim x(1 To 4) As Integer
    Dim y(1 To 13) As Integer
    a = "黑桃"
    b = "紅心"
    c = "紅鑽"
    d = "黑花"
    now = False
    Do Until now = True
    For j = 1 To 4
    For i = 1 To 13
    x = Int(Rnd() * 3) + 1
    y = Int(Rnd() * 12 + 1)
    Select Case x
    Case Is = 1
    Print #1, a & y
    Case Is = 2
    Print #1, b & y
    Case Is = 3
    Print #1, c & y
    Case Is = 4
    Print #1, d & y
    End Select
    Next i
    Next j
    Loop
    Close #1
    End
    End Sub

    回覆刪除
  2. --------------------------------------------
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in2.txt" For Input As #1
    Open App.Path & "\out2.txt" For Output As #2
    Input #1, n
    Print #2, S(n)
    Close
    Close
    End
    End Sub
    Function S(x)
    If x = 1 Then S = 10
    If x = 2 Then S = -3
    If x >= 3 Then
    S = S(x - 1) - S(x - 2)
    End If
    End Function
    ------------------------------------------------

    ------------------------------------------------
    Private Sub Form_Load()
    Me.Hide
    Dim c(51) As String
    A = Split("A,2,3,4,5,6,7,8,9,10,J,Q,K", ",")
    b = Split("黑桃,紅心,紅鑽,黑花", ",")
    d = 0
    For i = 0 To 3
    For j = 0 To 12
    c(d) = b(i) & A(j)
    d = d + 1
    Next
    Next
    End
    End Sub

    回覆刪除
  3. ---1---
    Private Sub Form_Load()
    Me.Hide
    Dim a As Double
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out1.txt" For Output As #2
    Max = 0
    a = 0
    Do While Not EOF(1)
    Line Input #1, x
    x = x + 10000000000#
    List1.AddItem x
    a = a + 1
    Loop
    For i = a - 1 To 1 Step -1
    b = List1.List(i) - 10000000000#
    Print #2, b
    Next
    Close
    Close
    End
    End Sub

    ---2---
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in2.txt" For Input As #1
    Open App.Path & "\out2.txt" For Output As #2
    Input #1, x
    Print s(x)
    Print #2, s(x)
    Close
    Close
    End
    End Sub
    Function s(n)
    If n = 1 Then s = 10
    If n = 2 Then s = -3
    If n >= 3 Then
    s = s(n - 1) - s(n - 2)
    End If
    End Function

    ---3---
    Private Sub Form_Load()
    Dim t As Boolean
    Me.Hide
    Open App.Path & "\out3.txt" For Output As #1
    t = True
    For i = 1000 To 9999
    ans = 0
    For j = 1 To 3
    a = Mid(i, j, 1)
    ans = ans + a ^ 3
    If ans = i Then
    Print #1, i
    Else
    t = False
    End If
    Next
    Next
    If t = False Then Print #1, "0000"
    Close
    End
    End Sub
    ---4---
    Dim u As Boolean
    Dim b, c, d, a, t, j
    Dim s(4, 13), s1(4, 13)
    Private Sub Form_Load()
    Randomize Timer
    Me.Hide
    Open App.Path & "\out4.txt" For Output As #1
    a = Split("0 1 2 3 4 5 6 7 8 9 10 j Q K")
    d = Split("0 黑桃 紅心 紅鑽 黑花")
    t = 0
    For j = 1 To 4
    For i = 1 To 13
    t = t + 1
    b = Int(Rnd() * 13 + 1)
    c = Int(Rnd() * 4 + 1)
    s(j, i) = d(c) & a(b)
    Call re
    If u = False Then
    Call re
    End If
    Next
    Next
    For j = 1 To 4
    For i = 1 To 13
    Print #1, s(j, i),
    Next
    Print #1, " "
    Next
    Close
    End
    End Sub

    Sub re()
    u = True
    Do
    For f = 1 To t
    If s(j, i) = List1.List(f) Then u = False
    b = Int(Rnd() * 13 + 1)
    c = Int(Rnd() * 4 + 1)
    s(j, i) = d(c) & a(b)
    If s(j, i) <> List1.List(f) Then u = True
    Next
    Loop Until u = True
    List1.AddItem s(j, i)
    End Sub

    回覆刪除

  4. 1.x 2.o 3.x 4.x
    晟晟
    1.x 2.o 3.x 4.x
    小冰
    1.o 3.o 3.x 4.x

    回覆刪除
  5. 第四題:
    Dim ans(52)
    Dim t As Boolean
    Private Sub Form_Load()
    Randomize Timer
    Me.Hide
    Open App.Path & "\out4.txt" For Output As #1
    a = Split("1 2 3 4 5 6 7 8 9 10 j Q K")
    b = Split("黑桃 紅心 紅鑽 黑花")
    For i = 1 To 52
    For j = i Mod 4 To i Mod 4
    For z = i Mod 13 To i Mod 13
    ans(i) = b(j) & a(z)
    Next
    Next
    Next
    Do
    t = True
    n = Int(Rnd() * 52 + 1)
    For i = 0 To 52
    If List2.List(i) = ans(n) Then t = False
    Next
    If t = True Then
    List2.AddItem ans(n)
    Print #1, ans(n) & " ";
    If List2.ListCount Mod 13 = 0 Then Print #1,
    End If
    Loop Until List2.ListCount = 52
    Close
    End
    End Sub

    這次改過應該正確了。
    一開始的想法太複雜,重新思考後,就比較容易了。
    第三題 題目看不太懂!

    回覆刪除
  6. 第四題:
    Private Sub Form_Load()
    Randomize Timer
    Me.Hide
    Open App.Path & "\out.txt" For Output As #1
    Dim c(51) As String
    Dim num(51), a, i As Integer

    a = Split("A,2,3,4,5,6,7,8,9,10,J,Q,K", ",")
    b = Split("黑桃,紅心,紅鑽,黑花", ",")
    d = 0
    For i = 0 To 3
    For j = 0 To 12
    c(d) = b(i) & a(j)
    d = d + 1
    Next
    Next

    For i = 0 To 51
    RA:
    num(i) = Int(Rnd * 52)
    If i > 0 Then
    For a = 0 To i - 1
    If num(i) = num(a) Then GoTo RA
    Next a
    End If
    If (i > 1) And (i Mod 13 = 0) Then
    Print #1, ""
    Print #1, c(num(i)) & " ";
    Else
    Print #1, c(num(i)) & " ";
    End If
    Next i

    Close #1
    End
    End Sub

    回覆刪除
  7. 第一題:

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out1.txt" For Output As #2
    Do While Not EOF(1)
    Line Input #1, n
    n = n + 10000000000#
    List.AddItem n
    Loop
    a = List.ListCount
    For i = a - 1 To 0 Step -1
    Print #2, List.List(i) - 10000000000#
    Next i
    Close
    Close
    End
    End Sub

    回覆刪除
  8. 第一題

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out1.txt" For Output As #2
    Dim n As Double
    n = 0
    Do Until EOF(1)
    Line Input #1, x
    x = x + 10000
    List1.AddItem x
    n = n + 1
    Loop
    For i = n - 1 To 0 Step -1
    b = List1.List(i) - 10000
    Print #2, b
    Next i
    Close #2
    Close #1
    End
    End Sub

    第三題

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out3.txt" For Output As #1
    For i = 100 To 9999
    c = 0
    ii = i
    For a = 1 To 4
    b = ii Mod 10
    ii = ii \ 10
    c = c + b ^ 3
    Next a
    If i = c Then Print #1, c
    Next i
    Close #1
    End
    End Sub

    回覆刪除