www.ypnh.net > VB如何求水仙花数

VB如何求水仙花数

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

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

For i = 100 To 999 If (i \ 100) ^ 3 + ((i Mod 100) \ 10) ^ 3 + (i Mod 10) ^ 3 = i Then Print i Next

Private Sub Form_Click() Dim q As Integer Dim b As Integer Dim s As Integer Dim n As Integer For n = 100 To 999 q = n \ 100 '求出百位数字 b = (n - q * 100) \ 10 '求出十位数字 s = n Mod 10 '求出个位数字 If ((q * q * q + b * b * ...

dim i as integer,m as integer for i=100 to 999 m=(i mod 10)^3 +(i \10 mod 10)^3+(i\100)^3 if m=i then print i next i 运行结果:153 370 371 407

所谓的水仙花数(梅花数)是指在三位整数(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, 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%, 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

直接用枚举法。 #include "stdio.h" void main() { int a,b,c,i; for(i=100;i

Private Sub Command1_Click() For i = 100 To 999 a = Mid(i, 1, 1) b = Mid(i, 2, 1) c = Mid(i, 3, 1) If a ^ 3 + b ^ 3 + c ^ 3 = i Then Debug.Print i & "=" & a & "^3+" & b & "^3+" & c & "^3" End If Next i End Sub

网站地图

All rights reserved Powered by www.ypnh.net

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