vb中关于水仙花数的题

来源:百度知道 编辑:UC知道 时间:2024/06/23 23:20:33
1)单击"读数据"按钮,则把文件夹中100个0到999之间的整数读入数组a中
2)单击"计算"按钮,则找出这100个整数中的所有水仙花数,并将它们的最大值与最小值分别显示在文本框text1,text2中
Dim a(100) As Integer
Private Sub Command1_Click()
Dim k As Integer
Open App.Path & "\in3.dat" For Input As #1
For k = 1 To 100
Input #1, a(k)
Next k
Close #1
End Sub

Private Sub Command2_Click()
'需考生编写

End Sub

'以下Function 过程用于判断某数是否为水仙花数
Function isnarc(p As Integer)
x = Fix(p / 100)
y = Fix((p - x * 100) / 10)
z = p - x * 100 - y * 10
If p = x ^ 3 + y ^ 3 + z ^ 3 Then
isnarc = True
Else
isnarc = False
End If
End Function

Private Sub Form_Unload(Cancel As Integer)
Open App.Path & "\out3.dat" For Output As #1
Print #1, Val(Text1.Text)
Print #1, Val(Text2.Text)
Close #1
End Sub
能帮忙

设置:max=-1,min=1000 是方便比较数的大小。因为你的数组中的数值最小的是0,最大的是999,那么,让max先=-1,小于你的最小值,在程序执行过程中,只要有水仙花数,他就会大于max,这样他的值才会附给max,min=1000也是同样的道理!

Dim a(100) As Integer
Private Sub Command1_Click()
Dim k As Integer
Open App.Path & "\in3.dat" For Input As #1
For k = 1 To 100
Input #1, a(k)
Next k
Close #1
End Sub

Private Sub Command2_Click()
'需考生编写

dim i as integer
dim max as integer,min as integer
max=-1:min=1000
for i=1 to 100
if isnarc(a(i)) then
print a(i)
if max<=a(i) then max=a(i)
if min>=a(i) then min=a(i)
end if
next
if max<>-1 then text1=max
if min<>1000 then text2=min
End Sub

'以下Function 过程用于判断某数是否为水仙花数
Function isnarc(p As Integer)
x = Fix(p / 100)
y = Fix((p - x * 100) / 10)
z = p - x * 100 - y * 10
If p = x ^ 3 + y ^ 3 + z ^ 3 Then
isnarc = Tru