admin 管理员组

文章数量: 887021


2024年1月23日发(作者:java invoke方法)

1)单击"读数据"按钮,则把文件夹中100个0到999之间的整数读入数组a中0000002)单击"计算"按钮,则找出这100个整数中的所有水仙花数,并将它们的最大值与最小值分别显示在文本框text1,text2中

0000Dim a(100) As Integer Dim k As Integer000000

Private Sub Command1_Click()00000000

0000 Open & "" For Input As #1 For k = 1 To 100

0000 Input #1, a(k) Next k Close #1End Sub

0000

000000

Private Sub Command2_Click() '需考生编写00000000

End Sub

'以下Function 过程用于判断某数是否为水仙花数

Function isnarc(p As Integer)

0000

x = Fix(p / 100)y = Fix((p - x * 100) / 10)z = p - x * 100 - y * 10If p = x ^ 3 + y ^ 3 + z ^ 3 Then

000000

0000000 isnarc = True Else0000

0000 isnarc = False End IfEnd Function

0000

Private Sub Form_Unload(Cancel As Integer)00000 Open & "" For Output As #1 Print #1, Val()

Print #1, Val() Close #10000

000000

End Sub

问题补充:

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

0000

Dim a(100) As Integer

0000

Private Sub Command1_Click()

Dim k As Integer

Open & "" For Input As #1

For k = 1 To 100

Input #1, a(k)

Next k

Close #1

End Sub

0000

00000

Private Sub Command2_Click()

'需考生编写

dim i as integer

00000000

00000dim max as integer,min as integer

max=-1:min=1000

for i=1 to 100

if isnarc(a(i)) then

print a(i)

0000

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

000000

0000End Sub

'以下Function 过程用于判断某数是否为水仙花数

Function isnarc(p As Integer)

x = Fix(p / 100)

y = Fix((p - x * 100) / 10)

z = p - x * 100 - y * 10

0000

000000

If p = x ^ 3 + y ^ 3 + z ^ 3 Then

isnarc = True

Else

isnarc = False

End If

End Function

000000

0000Private Sub Form_Unload(Cancel As Integer)

Print #1, Val()

Print #1, Val()

Close #1

00000Open & "" For Output As #1

0000

End Sub0000

所谓的水仙花数(梅花数)是指在三位整数(100到999之间)中,百位数、十位数、个位数的立方和等于它本身,如153=1^3+5^3+3^3。

程序代码如下:

Private Sub Command1_Click()0000

Dim i As Integer, s As Integer0000 Dim a As Integer, b As Integer, c As Integer Print "100到999所有水仙花数(也叫梅花数):"; For i = 100 To 999 a = i 100 '取百位数

00000000 b = i 10 Mod 10 '或 b = i Mod 100 10 取十位数 c = i Mod 10 ‘取个位数 s = a ^ 3 + b ^ 3 + c ^ 3 '水仙花数的判断依据 If s = i Then Print i; End If Next iEnd Sub

运行结果:0000

0000000

0000

0000

000000

0000000

0000

00100到999所有水仙花数(也叫梅花数): 153 370 371 407

VB 100到2000中有多少个水仙花数

满意回答

0000000

0000000

Option Explicit

000000

Private Sub Form_Click()

Dim N As Integer, i As Integer, s As String, l As Integer, Sum As Long For N = 100 To 2000

0000

s = CStr(N) l = Len(s)000000

0000

00000 Sum = 0 For i = 1 To l0000000

0000 Sum = Sum + (CInt(Mid(s, i, 1))) ^ l Next

00000 If Sum = N Then0000000

Print Mid(s, 1, 1) & "^" & l; For i = 2 To l Next00000000

0000 Print " + " & Mid(s, i, 1) & "^" & l;00000

Print " = " & N End If Next

00000

End Sub0000

运行结果:0000000

1^3 + 5^3 + 3^3 = 1533^3 + 7^3 + 0^3 = 3703^3 + 7^3 + 1^3 = 3710000004^3 + 0^3 + 7^3 = 4071^4 + 6^4 + 3^4 + 4^4 = 1634


本文标签: 按钮 个位数 数组 整数 用于