輸入一大於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
Dim a(999), Min, Max, x, y As Double
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, x
a(1) = x
Print #2,x, x ^ 2
x = x ^ 2
n = 2
Do
Max = 0
Min = 10
k = Len(x)
For i = 1 To k
c = Val(Mid(x, i, 1))
If c > Max Then Max = c
If c < Min Then Min = c
Next i
x = Min * 10 + Max
a(n) = x
For i = 1 To n - 1
If x = a(i) Then
Print #2, "*" & x, x ^ 2
Exit Sub
End If
Next i
Print #2, x, x ^ 2
x = x ^ 2
n = n + 1
Loop
Close #1
Close #2
End Sub
輸入 8
輸出
8 64
46 2116
16 256
26 676
67 4489
49 2401
4 16
*16 256
阿瑋好,
回覆刪除1、在exit sub之前,還是先close #2/close #1,比較好。
2、程式完全依照題意說明前進,蠻順的,很好。
3、既然之前說過,反正就是要「結果」「快」,那麼用
c = val(mid(x,i,1))
和
c = x mod 10
x = x \10
也沒什麼關係了。知道有這兩種方式都可以,就好了。
Dim a(100)
回覆刪除Private Sub Form_Load()
Open App.Path & "/in.txt" For Input As #1
Input #1, x
Close #1
Open App.Path & "/out.txt" For Output As #2
a(0) = x
b = a(0) ^ 2
Print #2, a(0); b
For i = 1 To 99
Max = 0
Min = 9
For j = 1 To Len(b)
c = Mid(b, j, 1)
If Val(c) > Max Then Max = c
If Val(c) < Min Then Min = c
Next j
a(i) = Val(Min) * 10 + Val(Max)
For k = 0 To i - 1
If a(k) = a(i) Then Print #2, "*" & a(i): Exit Sub
Next k
b = a(i) ^ 2
Print #2, a(i); b
Next i
Close #2
End Sub
最後找到重複用Exit Sub並不會影響結果
所以還是用了下去
這裡卡好久== 我不知道 9不等於"9" 說@@
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Line Input #1, x
Close #1
st = ""
Do
If Len(x) = 1 Then
st = st & "0" & x
Else
st = st & x
End If
Print #2, x,
x = x ^ 2
Print #2, x
Max = 1: Min = 9: y = x
For i = 1 To Len(x)
c = y Mod 10
If c > Max Then Max = c
If c < Min Then Min = c
y = y \ 10
Next i
For j = 1 To Len(st) \ 2
q = Val(Mid(st, j * 2 - 1, 1))
w = Val(Mid(st, j * 2, 1))
If (Min = q) And (Max = w) Then
Print #2, "*" & Val(Min & Max), (Val(Min & Max)) ^ 2
Exit Do
Else
End If
Next j
x = Val(Min & Max)
Loop
Close #2
End Sub
輸入
9
輸出
9 81
18 324
24 576
57 3249
29 841
*18 324
Dim c As Integer
回覆刪除Dim aaNew(1000) As Integer
Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, a
aaNew(0) = a
aa = a ^ 2
Print aaNew(0), aa
q = 0
Do
If q > 0 Then
c = aaNew(q - 1) ^ 2
bb = c
Amax = 0
Amin = 10
For i = 1 To Len(bb)
x = Val(Mid(c, i, 1))
If x > Amax Then Amax = x
If x < Amin Then Amin = x
Next i
aaNew(q) = Amin * 10 + Amax
For i = 0 To q
If aaNew(q) = aaNew(i) And q <> i Then
Print "*" & aaNew(q), aaNew(q) ^ 2
Exit Do
End If
Next i
Print aaNew(q), aaNew(q) ^ 2
q = q + 1
Else
q = q + 1
End If
Loop
Close #2
Close #1
End Sub
老師
這題在做的時候
我原本是 For i = 1 To Len(c)
可是我發現,c改變了以後
Len(c)卻沒有變
(這點真的是害我做很久做不出來= =")
Dim c As Integer
回覆刪除Dim aaNew(1000) As Integer
Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, a
aaNew(0) = a
aa = a ^ 2
Print #2, aaNew(0), aa
q = 0
Do
If q > 0 Then
c = aaNew(q - 1) ^ 2
bb = c
Amax = 0
Amin = 10
For i = 1 To Len(bb)
x = Val(Mid(c, i, 1))
If x > Amax Then Amax = x
If x < Amin Then Amin = x
Next i
aaNew(q) = Amin * 10 + Amax
For i = 0 To q
If aaNew(q) = aaNew(i) And q <> i Then
Print #2, "*" & aaNew(q), aaNew(q) ^ 2
Exit Do
End If
Next i
Print #2, aaNew(q), aaNew(q) ^ 2
q = q + 1
Else
q = q + 1
End If
Loop
Close #2
Close #1
End Sub
(忘了改#2了= =")
小白好,
回覆刪除1你說的第一種狀況,不應該會出現的,將原來的程式,放出來看看吧。
2程式中先設了q=0 然後在do後面再去說如果q<0 做了什麼事,幹麻那麼麻煩啊。
你是不是該直接設q=1再進入do迴圈。
3你的程式流程也是「不結構化」,還用exit do直接跳出兩層迴圈,程式一大,很容易出錯的。
4不過,這個程式結果應該是ok的。
Dim c As Integer
回覆刪除Dim aaNew(1000) As Integer
Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, a
aaNew(0) = a
aa = a ^ 2
Print aaNew(0), aa
q = 0
Do
If q > 0 Then
c = aaNew(q - 1) ^ 2
Amax = 0
Amin = 10
For i = 1 To Len(c)
x = Val(Mid(c, i, 1))
If x > Amax Then Amax = x
If x < Amin Then Amin = x
Next i
aaNew(q) = Amin * 10 + Amax
For i = 0 To q
If aaNew(q) = aaNew(i) And q <> i Then
Print "*" & aaNew(q), aaNew(q) ^ 2
Exit Do
End If
Next i
Print aaNew(q), aaNew(q) ^ 2
q = q + 1
Else
q = q + 1
End If
Loop
Close #2
Close #1
End Sub
原本的
小白好,(還有大家好,一塊兒看看吧),
回覆刪除以前就一直排斥用字串的函數來套在數字上。
那得靠vb本身的「容錯」來得到函數的結果。
這次又靠小白的錯,來發現了這一個容錯的錯。
在字串中 len 這個函數當然是傳回字串的長度。
但是,如果是其它種類的變數的話,則是傳回其變數記憶體的長度,整數2,長整數4,單精4,倍精8。
於是,要能真的傳回數字的長度的,就不能先設定變數的種類,就是用variant,才會「剛好」傳回數字的長度。
請看下例:
Private Sub Form_Load()
Dim a As Integer
Dim b
Dim c As Long
Dim d As Single
Dim e As Double
a = 300
b = 300
c = 300
d = 300
e = 300
Print Len(a), Len(b), Len(c), Len(d), Len(e)
End Sub
那麼,a,b,c,d,e的長度,各是多少呢? 試試吧
Public 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
st = " "
Do
y = " " & x & " "
If InStr(st, y) = 0 Then
Print #2, x, x ^ 2
st = st & " " & x
x = x ^ 2
num = x
Max = 1: Min = 99
Do Until num = 0
a = num Mod 10
num = num \ 10
If a > Max Then Max = a
If a < Min Then Min = a
Loop
x = Val(Min & Max)
Else
Print #2, "*"; x, x ^ 2
Exit Sub
End If
Loop
Close #1
Close #2
End
End Sub
9M4