小小练习大神掠过吧
题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
1 public static void main(String[] args) { 2 for(int i=100;i<1000;i++){ 3 if(value(i)){ 4 System.out.println("i ="+i); 5 } 6 } 7 8 } 9 private static boolean value(int i) { 10 boolean temp=false; 11 int b=i/100; 12 int s=i%100/10; 13 int g=i%10; 14 int sum=(int) (Math.pow(b, 3)+Math.pow(s, 3)+Math.pow(g, 3)); 15 if(sum==i){ 16 temp=true; 17 } 18 return temp; 19 20 }
原文:http://www.cnblogs.com/bequt/p/5158698.html