Private Sub Form_Activate() Dim xx(9) As Integer Open App.Path & "\in.txt" For Input As #1 Input #1, x, y Close #1 ans$ = x * y num = Len(ans$) For i = 1 To num X1 = Mid(ans$, i, 1) Select Case X1 Case 0 xx(0) = xx(0) + 1 Case 1 xx(1) = xx(1) + 1 Case 2 xx(2) = xx(2) + 1 Case 3 xx(3) = xx(3) + 1 Case 4 xx(4) = xx(4) + 1 Case 5 xx(5) = xx(5) + 1 Case 6 xx(6) = xx(6) + 1 Case 7 xx(7) = xx(7) + 1 Case 8 xx(8) = xx(8) + 1 Case 9 xx(9) = xx(9) + 1 End Select Next i Open App.Path & "/out.txt" For Output As #1 x = 0 For i = 0 To 9 If xx(i) = 0 Then Else Print #1, x; Print #1, xx(i) & " "; End If x = x + 1 Next i Close #1 End Sub
D im A(10) As Integer '部落格中無法宣告= = Private Sub Form_Activate() Open App.Path & "\in.txt" For Input As #1 Input #1, x, y Sum = x * y For i = 1 To Len(Sum) k = Mid(Sum, i, 1) A(k) = A(k) + 1 Next i Open App.Path & "/out.txt" For Output As #2 For j = 0 To 9 If A(j) <> 0 Then Print #2, j & A(j) & " "; Next j Close #2 Close #1 End Sub
Private Sub Form_Activate() Open App.Path & "\in.txt" For Input As #1 Input #1, x, y z = x * y For i = 1 To Len(z) c = Val(Mid(z, i, 1)) For j = 0 To 9 b(j) = j If c = j Then a(j) = Val(a(j)) + 1 Next j Next i Close #1 Open App.Path & "/out.txt" For Output As #2 For i = 0 To 9 If Val(a(i)) = 0 Then b(i) = "" If Val(a(i)) = 0 Then a(i) = "" Print #2, a(i) & b(i) Next i Close #2 End Sub
Dim num(9) As Integer 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, y ans = x * y For i = 1 To Len(Str(ans)) - 1 k = Mid(ans, i, 1) num(Val(k)) = num(Val(k)) + 1 Next i For i = 0 To 9 If num(i) <> 0 Then Print #2, i & num(i) & " "; Next i Close #2 Close #1 End End Sub
熊掌:
回覆刪除open app.path & "in.txt" for input as #1
input #1,x,y
可以將以空白或逗號隔開的兩個數,一次讀入
Private Sub Form_Activate()
回覆刪除Dim xx(9) As Integer
Open App.Path & "\in.txt" For Input As #1
Input #1, x, y
Close #1
ans$ = x * y
num = Len(ans$)
For i = 1 To num
X1 = Mid(ans$, i, 1)
Select Case X1
Case 0
xx(0) = xx(0) + 1
Case 1
xx(1) = xx(1) + 1
Case 2
xx(2) = xx(2) + 1
Case 3
xx(3) = xx(3) + 1
Case 4
xx(4) = xx(4) + 1
Case 5
xx(5) = xx(5) + 1
Case 6
xx(6) = xx(6) + 1
Case 7
xx(7) = xx(7) + 1
Case 8
xx(8) = xx(8) + 1
Case 9
xx(9) = xx(9) + 1
End Select
Next i
Open App.Path & "/out.txt" For Output As #1
x = 0
For i = 0 To 9
If xx(i) = 0 Then
Else
Print #1, x;
Print #1, xx(i) & " ";
End If
x = x + 1
Next i
Close #1
End Sub
D im A(10) As Integer '部落格中無法宣告= =
回覆刪除Private Sub Form_Activate()
Open App.Path & "\in.txt" For Input As #1
Input #1, x, y
Sum = x * y
For i = 1 To Len(Sum)
k = Mid(Sum, i, 1)
A(k) = A(k) + 1
Next i
Open App.Path & "/out.txt" For Output As #2
For j = 0 To 9
If A(j) <> 0 Then Print #2, j & A(j) & " ";
Next j
Close #2
Close #1
End Sub
Private Sub Form_Activate()
回覆刪除Dim a(9)
Open App.Path & "\in.txt" For Input As #1
Input #1, x, y
ans = x * y
For i = 1 To Len(ans)
w = Mid(ans, i, 1)
a(w) = a(w) + 1
Next i
Close #1
Open App.Path & "\out.txt" For Output As #2
For i = 0 To 9
If a(i) <> 0 Then
Print #2, i & a(i) & " ";
End If
Next i
Close #2
End Sub
Y揚,高仔,皓好,
回覆刪除你們三個的程式都OK,
但是,你們都將用來處理字串的函數mid, len拿來處理數字,並不是很好的方式。
改進的方式是該用mod 10來取出最後一個數字,\10去掉一個數字,直到沒有數字了。
試試吧。
不曉得這樣算不算錯 輸出成直的 不過應該沒問題吧(汗
回覆刪除D im a(9), b(9) As String
Private Sub Form_Activate()
Open App.Path & "\in.txt" For Input As #1
Input #1, x, y
z = x * y
For i = 1 To Len(z)
c = Val(Mid(z, i, 1))
For j = 0 To 9
b(j) = j
If c = j Then a(j) = Val(a(j)) + 1
Next j
Next i
Close #1
Open App.Path & "/out.txt" For Output As #2
For i = 0 To 9
If Val(a(i)) = 0 Then b(i) = ""
If Val(a(i)) = 0 Then a(i) = ""
Print #2, a(i) & b(i)
Next i
Close #2
End Sub
松鼠好,
回覆刪除print #2,a(i)&b(i)
->
print #2,a(i)&b(i);
就可以了。
Private Sub Form_Load()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Input #1, x, y
Close #1
Dim z(9) As Integer
ans = x * y
txt = "0123456789"
a = Len(ans)
Open App.Path & "\out.txt" For Output As #2
For i = 1 To a
b = ans Mod 10
c = InStr(txt, b) - 1
z(c) = z(c) + 1
ans = ans \ 10
Next i
For i = 0 To 9
If z(i) <> 0 Then
Print #2, i & z(i) & " ";
End If
Next i
Close #2
End Sub
阿瑋好,
回覆刪除似乎用txt是你的密技哦,不錯。
只是
b = ans Mod 10
c = InStr(txt, b) - 1
z(c) = z(c) + 1
這三行是不是多遶了一層呢? 用b來當成c就可以了吧?
松鼠的這個部分,也是多做了一個不必要的迴圈,再想想。
這題其實也可以用切割的方式做出來
回覆刪除雖然電腦本身運算比較多
但是程式碼簡短
Dim num(9) As Integer
回覆刪除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, y
ans = x * y
For i = 1 To Len(Str(ans)) - 1
k = Mid(ans, i, 1)
num(Val(k)) = num(Val(k)) + 1
Next i
For i = 0 To 9
If num(i) <> 0 Then Print #2, i & num(i) & " ";
Next i
Close #2
Close #1
End
End Sub
4分24秒