2012年11月5日 星期一

數列

輸入一大於1的正整數。平方後取其最小的數字為十位數(若為0則無十位數),最大的為個位數,組成一個2位數,再將此數平方,以上述方式再取得下一個數,直到重複才停止,請列出產生的數列,並標明重複的數。如輸入7則得下列結果:
輸入或產生的新數 平方 最小數 最大數 產生的新數
7 49 4 9 49
49 2401 0 4 4
4 16 1 6 16
16 256 2 6 26
26 676 6 7 67
67 4489 4 9 49
*49

※49重複則停止
輸入說明:大於1之正整數
輸出說明:依規則產生的數列,使用"*"標明重複產生之數字。

輸入範例:7

輸出範例:
7 49
49 2401
4 16
16 256
26 676
67 4489
*49 2401

3 則留言:

  1. Dim num, mix, min, strr(9999), mycount As Integer
    Dim repeat As Boolean
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, num
    repeat = False
    mycount = 0
    Do
    mix = 0
    min = 9999
    num = num ^ 2
    For i = 1 To Len(num)
    If Val(Mid(num, i, 1)) > mix Then mix = Val(Mid(num, i, 1))
    If Val(Mid(num, i, 1)) < min Then min = Val(Mid(num, i, 1))
    Next
    strr(mycount) = num ^ 0.5 & " " & num
    num = Val(min) & Val(mix)

    For ii = 0 To mycount
    If strr(mycount) = strr(ii) And mycount <> ii Then
    repeat = True
    strr(mycount) = "*" & strr(mycount)
    End If
    Next
    Print #2, strr(mycount)
    If repeat <> True Then mycount = mycount + 1
    Loop Until repeat = True

    Close #2
    Close #1
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As 2
    Input #1, x
    Do
    Print #2, Val(x);
    List3.AddItem x
    x = Val(x) ^ 2
    Print #2, Val(x)
    List2.AddItem x
    lon = Len(x)
    For i = 1 To lon
    w = Mid(x, i, 1)
    List1.AddItem w
    Next
    x = Val(List1.List(0) & List1.List(lon - 1))
    List1.Clear
    For j = 0 To List2.ListCount - 1
    If x ^ 2 = List2.List(j) And x = List3.List(j) Then Exit Do
    Next
    Loop
    Print #2, "*" & x;
    Print #2, x ^ 2
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    Do
    Print #2, x;
    List1.AddItem x
    x = x ^ 2
    Print #2, x
    For i = 1 To Len(x)
    List2.AddItem Mid(x, i, 1)
    Next
    x = Val(List2.List(0) & List2.List(List2.ListCount - 1))
    List2.Clear
    For i = 0 To (List1.ListCount - 1)
    If x = Val(List1.List(i)) Then Print #2, "*" & x & " " & x ^ 2: Exit Do
    Next
    List1.AddItem x
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除