VB如何求水仙花数
VB如何求水仙花数,把3位数的水仙花数字全部打印出来水仙花数字就是:158=1^3+2^3+3^也就是说把100-999的水仙花数都打印出来...
VB如何求水仙花数,把3位数的水仙花数字全部打印出来
水仙花数字就是:158=1^3+2^3+3^
也就是说把100-999的水仙花数都打印出来 展开
水仙花数字就是:158=1^3+2^3+3^
也就是说把100-999的水仙花数都打印出来 展开
4个回答
展开全部
水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3+ 3^3 = 153)
可以运用将一个 For...Next 循环放置在另一个 For...Next 循环中,组成嵌套循环来解决水仙花求解问题。
以3位10进制数100-999为例,代码如下:
Private Sub Command1_Click()
Dim i As Long, j As Long, k As Long
Dim s As Long
For i = 1 To 9
For j = 0 To 9
For k = 0 To 9
s = i * 100 + j * 10 + k
If ((i ^ 3) + (j ^ 3) + (k ^ 3)) = s Then
Debug.Print s
End If
Next k
Next j
Next i
End Sub
展开全部
错的不是这条语句,而是其他的,而且错得离谱。下面是正确的:
Private Sub Command1_Click()
Dim i As Integer, a As Integer, b As Integer, c As Integer, st As String
For a = 1 To 9
For b = 0 To 9
For c = 0 To 9
i = a * 100 + b * 10 + c
If i = a ^ 3 + b ^ 3 + c ^ 3 Then
st = i & " = " & a & "^3 + " & b & "^3 + " & c & "^3"
List1.AddItem st
End If
Next c
Next b
Next a
End Sub
Private Sub Command1_Click()
Dim i As Integer, a As Integer, b As Integer, c As Integer, st As String
For a = 1 To 9
For b = 0 To 9
For c = 0 To 9
i = a * 100 + b * 10 + c
If i = a ^ 3 + b ^ 3 + c ^ 3 Then
st = i & " = " & a & "^3 + " & b & "^3 + " & c & "^3"
List1.AddItem st
End If
Next c
Next b
Next a
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
for i=1 to 9
for j=0 to 9
for k=0 to 9
a=i*100+j*10+k
if ((i^3)+(j^3)+(k^3))=a then print a
next k
next j
next i
(主要程序段)
for j=0 to 9
for k=0 to 9
a=i*100+j*10+k
if ((i^3)+(j^3)+(k^3))=a then print a
next k
next j
next i
(主要程序段)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Form1_Click() Dim a As Integer, b As Integer, c As Integer For a = 1 To 9 For b = 0 To 9 For c = 0 To 9 If a ^ 3 + b ^ 3 + c ^ 3 = a * 100 + b * 10 + c Then Form1.Print a * 100 + b * 10 + c End If Next c Next b Next a End
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询