日期:2014-05-20  浏览次数:20621 次

各位来看看
一个求水仙花数的程序,但是就是求不出来
 希望各位都来帮忙看看哪里不对
package project1;

public class Class2 {
public static void main(String[] args) {
  int a,b,c,d;
  d=100;
  for(d=100;d<1000;d=d+1){
  a=d/100;
  b=(d-a*100)/10;
  c=d-a*100-b*10;
  if (d==a*a*a+b*b*b+c*c*c);
  System.out.println(d);
  }
}
}

------解决方案--------------------
首先提醒你结贴给分,0的结帖率……

下面输出1000以内的水仙花数
Java code


public class Shuixianhuashu {
    public static void main(String[] args) {
        for (int i = 100; i < 1000; i++) {
            int a = i / 100;
            int b = i / 10 % 10;
            int c = i % 10;
            if (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3) == i)
                System.out.println(i + "是水仙花数");
        }
    }

}

------解决方案--------------------
public class Test {

public static void main(String[] args) {
int a,b,c;

for(int i=100; i<1000; i++) {
a = i/100;
b = (i-a*100)/10;
c = i-a*100-b*10;
if(i == a*a*a + b*b*b + c*c*c) {
System.out.println(i);
}
}
}

}