一道Vb程序设计竞赛的练习题目,求高人指点

2024-12-31 11:54:32
推荐回答(1个)
回答1:

高人不敢当:
添加一个文本框和一个按钮,文本框输入N值
Option Explicit

Private Sub Command1_Click()
Dim i As Long
Dim j As Long
Dim m As Long
Dim W As Integer
Dim N As Long
Dim G As Integer
Command1.Enabled = False
G = 0
N = Val(Text1.Text)
For i = 1 To N - 1
j = 2
m = j * (N - i)
W = Len(CStr(i)) + Len(CStr(j)) + Len(CStr(m))
Do While W <= 9
If W = 9 Then
'i是整数部分,j是分子,m是分母
If Jug(i, j, m) Then G = G + 1: Debug.Print i, j, m
End If
j = j + 1
m = j * (N - i)
W = Len(CStr(i)) + Len(CStr(j)) + Len(CStr(m))
Loop
Next i
Print G
Command1.Enabled = True
End Sub

'判断是否满足条件--ii是整数部分,ji是分子,mm是分母
Private Function Jug(ByVal ii As Long, ByVal jj As Long, ByVal mm As Long) As Boolean
Dim Lins As String
Dim d(1 To 9) As Integer
Dim i As Integer
Dim j As Integer
Dim T As Integer
Lins = CStr(ii) & CStr(jj) & CStr(mm)
For i = 1 To 9
d(i) = Val(Mid(Lins, i, 1))
Next i
For i = 1 To 8
For j = i + 1 To 9
If d(i) > d(j) Then
T = d(i)
d(i) = d(j)
d(j) = T
End If
Next j
Next i
Lins = ""
For i = 1 To 9
Lins = Lins & CStr(d(i))
Next i
If Lins = "123456789" Then
Jug = True
Else
Jug = False
End If
End Function