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

VB如何求水仙花数

for i=1 to 9 for j=0 to 9 for k=0 to 9 a=i*100+j*10+k if ((i^3)+(j^3)+(k^3))=a then print a next k next jnext i(主要程序段)

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

Private Sub Form_Click() Dim i, j, m, n As Integer For n = 100 To 999 i = n \ 100 j = (n Mod 100) \ 10 m = n Mod 10 If i ^ 3 + j ^ 3 + m ^ 3 = n Then Print n; "="; i; "^3+"; j; "^3+"; m; "^3" End If Next nEnd Sub

需要在画面追加一个按钮控件:command1Private Sub Command1_Click()Print "水仙花数有:";For i = 100 To 999 If (i \ 100) ^ 3 + ((i Mod 100) \ 10) ^ 3 + (i Mod 10) ^ 3 = i Then Print i;Next iEnd Sub

Private Sub Form1_Click() Dim a As Integer, b As Integer, c As Integer For a = 1 To 9 For b = 0 To 9 For c = 0 To 9 If a ^ 3 + b ^ 3 + c ^ 3 = a * 100 + b * 10 + c Then Form1.Print a * 100 + b * 10 + c End If Next c Next b Next a End

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 iEnd Sub

Private Sub Form_Click()Dim a As IntegerDim b As IntegerDim c As IntegerDim d As IntegerFor d = 100 To 999a = d \ 100 '求出百位数字b = (d - a * 100) \ 10 '求出十位数字c = d Mod 10 '求出个位数字If (a ^ 3 + b ^ 3 + c ^ 3) = d Then Print dEnd IfNextEnd Sub

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

'求水仙花数Private Sub Form1_Click() Dim a As Integer, b As Integer, c As Integer For a = 1 To 9 For b = 0 To 9 For c = 0 To 9 If a ^ 3 + b ^ 3 + c ^ 3 = a * 100 + b * 10 + c Then Form1.Print a * 100 + b * 10 + c End If Next c Next b Next aEnd '求1~

///编程求出所有的 “水仙花数”:2006-10-10 08:43/// 把下面代码复制到代码区,点击窗体触发事件 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 + 10

网站地图

All rights reserved Powered by www.ypnh.net

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