打印水仙花数

题意:打印出所有的水仙花数,所谓水仙花数是指一个三位数,其各位数字立方和等于该数本身,例如:153是一个水仙花数,因为153=1的三次方+5的三次方+3的三次方。

java

public class Program {  
    public static void main(String args[]) {
        int b1,b2,b3;
            for(int m=101;m<1000;m++) {
                b3=m/100;
                b2=m%100/10; 
                b1=m%10;
                if(b3*b3*b3+b2*b2*b2+b1*b1*b1==m) {
                    System.out.println(m+"是一个水仙花数");
                }
            }
    }
}

C

#include <stdio.h>
void main(){
    int b1,b2,b3;
    for(int m=101;m<1000;m++){
     b1=m/100;
     b2=m%100/10;
  b3=m%10;
  if(b1*b1*b1+b2*b2*b2+b3*b3*b3==m){
      printf("%d是一个水仙花数\n",m);
  }
 }
}

VB

打印水仙花数

写在command1的click事件里

For m = 101 To 999
    a = Left(m, 1)
    b = Mid(m, 2, 1)
    c = Right(m, 1)
    If a ^ 3 + b ^ 3 + c ^ 3 = m Then
        Text1.Text = Text1.Text + CStr(m) & " "
    End If
Next m

相关文章: