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

VB如何求水仙花数

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

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 Command1_Click() Dim s As Integer, a As Integer, b As Integer, c As Integer s = Val(Text1.Text) If Trim(Str(s)) = Text1.Text And Len(Text1.Text) = 3 Then a = s \ 100 b = s \ 10 Mod 10 c = s Mod 10 If s = a ^ 3 + 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 ...

很简单的 新建一个 默认按钮 Command1 Private Sub Command1_Click() Dim a As Integer, b As Integer, c As Integer, i As Integer For i = 100 To 999 a = Int(i / 100) b = Int(i / 10 - a * 10) c = i Mod 10 If i = a ^ 3 + b ^ 3 + c ^ 3 ...

写个代码(VB.net),参考一下: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim I As Integer Dim A As Integer, B As Integer, C As Integer '定义几个变量,其中A、B、C...

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() For i = 100 To 999 j = Left(i, 1) k = Mid(i, 2, 1) l = Right(i, 1) If j ^ 3 + k ^ 3 + l ^ 3 = i Then Print i End If Next i End Sub 输出后总共只有4个 153 370 371 407 1000以内就是这么多

网站地图

All rights reserved Powered by www.ypnh.net

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