2012年11月20日 星期二

VB101小考 第1次 3rd

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

2 則留言:

  1. Dim haveprint As Boolean
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out3.txt" For Output As #2
    For i = 1000 To 9999
    For ii = 1 To 4
    ans = ans + Val(Mid(i, ii, 1)) ^ 3
    Next
    If ans = i Then Print #2, ans: haveprint = True
    ans = 0
    Next
    If haveprint = False Then Print #2, "0000"
    Close #2
    End
    End Sub

    回覆刪除
  2. Dim b As Boolean
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out3.txt" For Output As #1
    For i = 1000 To 9999
    b = False
    For j = 1 To 4
    x = x + (Val(Mid(i, j, 1))) ^ 3
    Next
    If x = i Then b = True: Print #1, i
    Next
    If b = False Then Print #1, "0000"
    Close
    End
    End Sub

    回覆刪除