vb题,求解答6. 编一程序,显示出所有的水仙花数.所谓水仙花数,是指一个3位数,其各位数字立方和等于该数字本身.例如,

1个回答

  • Sub Main()

    Dim tmp As Integer, i As Integer, j As Integer, k As Integer

    For i = 1 To 9

    For j = 0 To 9

    For k = 0 To 9

    tmp = (i * 100 + j * 10 + k)

    If tmp = i ^ 3 + j ^ 3 + k ^ 3 Then

    Console.Write(Str(tmp) + vbTab)

    End If

    Next

    Next

    Next

    Console.WriteLine()

    Console.ReadKey()

    End SubSub Main()

    Dim num As Integer, i As Integer, j As Integer, k As Integer

    For num = 100 To 999

    i = Int(num / 100)

    j = Int((num - i * 100) / 10)

    k = num - i * 100 - j * 10

    If i ^ 3 + j ^ 3 + k ^ 3 = num Then Console.Write(Str(num) + vbTab)

    Next

    Console.WriteLine()

    Console.ReadKey()

    End Sub

    153,370,371,407

相关问题