www.ypnh.net > 用vB求1到1000水仙花数的编程方法

用vB求1到1000水仙花数的编程方法

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

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

Private Sub Command1_Click() Dim n As Integer, i As Integer, s As String Dim A As Integer, B As Integer, C As Integer n = 0 '这个地方n是统计水仙花数的个数的变量,开始时应为0,而不是1. For i = 100 To 999 s = Trim(Str$(i)) A = Va...

Private Sub Form_Load() Me.AutoRedraw = True Dim sum For i = 100 To 999 sum = 0 For j = 2 To 4 sum = sum + Val(Mid(Str(i), j, 1)) ^ 3 Next j If sum = i Then Print i Next i End Sub

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

很简单的 新建一个 默认按钮 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 ...

Dim i%, j%, m% s = i * 100 + j * 10 * m '一定=0 Do While s > 0 '一定不成立 '"水仙花数" Private Sub Form_Click() Dim a, b, c As Integer 'a(个)b(十)c(百) For a = 0 To 9 For b = 0 To 9 For c = 1 To 9 If a ^ 3 + b ^ 3 + c ^ 3 = a +...

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

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 * ...

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