www.ypnh.net > 用VB求三位数的水仙花数

用VB求三位数的水仙花数

Private Sub Command1_Click() Dim a%, b%, sum As Integer, c As String For a = 100 To 999 c = CStr(a) sum = 0 For b = 1 To 3 sum = sum + Val(Mid(c, b, 1)) ^ 3 Next b If sum = a Then Print a End If Next a End Sub

回答这题目还需要先去百度“水仙花数”亲测可用,如上Private Sub Command1_Click()Dim i, j, m, n, sum As IntegerDim arr(1000) As IntegerDim a, s As Integera = 1For n = 100 To 999i = n \ 100j = (n Mod 100) \ 10m = n Mod 10If i ^ 3 + ...

两种方法: 1、用三重循环,将三个数连接成一个3位数再进行判断。 2、用一重循环,将所有的三位数逐位分离后再进行判断。 Private Sub Command1_Click() Cls Print"第一种方法:" Dim a As Integer,b As Integer,c As Integer,d As Integer For ...

水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3+ 3^3 = 153) 可以运用将一个 For...Next 循环放置在另一个 For...Next 循环中,组成嵌套循环来解决水仙花求解问题。 以3位10进制数100-999为...

所谓的水仙花数(梅花数)是指在三位整数(100到999之间)中,百位数、十位数、个位数的立方和等于它本身,如153=1^3+5^3+3^3。 程序代码如下: Private Sub Command1_Click() Dim i As Integer, s As Integer Dim a As Integer, b As Integer, c ...

Private Sub Command1_Click() Dim i As Integer, s As Integer Dim sum As Integer Dim a As Integer, b As Integer, c As Integer Print "100到500所有水仙花数(也叫梅花数):"; For i = 100 To 500 a = i \ 100 '取百位数 b = i \10 Mod 10...

Private Sub Command1_Click()Dim i, j, m, n, sum As IntegerFor n = 100 To 999i = n \ 100j = (n Mod 100) \ 10m = n Mod 10If i ^ 3 + j ^ 3 + m ^ 3 = n Thensum = sum + nEnd IfNext nPrint "求100-999之间水仙花数之和为:" & sumEnd Sub

Private Sub Command1_Click() Dim a%, b%, c As Integer For a = 1 To 9 b = 0 Do While b 9 If a ^ 3 + b ^ 3 + c ^ 3 = 100 * a + 10 * b + c Then Print "水仙花数"; 100 * a + 10 * b + c End If c = c + 1 Loop b = b + 1 Loop Next a End...

Private Sub Command2_Click() '水仙花数 153=1*1*1+5*5*5+3*3*3For i = 10 To 999If i < 100 Then ge = i Mod 10 shi = i \ 10 If ge ^ 3 + shi ^ 3 = i Then Print ,Else ge = i Mod 10 shi = i \ 10 Mod 10 bai = i \ 100 If ge * ge * ge + ...

输出所有的水仙花数,把谓水仙花数是指一个数3位数,其各各位数字立方和等于其本身, 例如: 153 = 1*1*1 + 3*3*3 + 5*5*5 class DafodilNumber{ public static void main(String[] args){ System.out.println(“以下是所有的水仙花数”); int num...

网站地图

All rights reserved Powered by www.ypnh.net

copyright ©right 2010-2021。
www.ypnh.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com