天天看点

用java打印出三位数中的“水仙花数”

public class Test02 {
	public static void main(String[] args){
		/*打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,
		 * 其各位数字立方和等于该数本身。
		 * 例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
		 * */
		int count=0;
		for(int n=100;n<=999;n++){
			int bai=n/100;
			int shi=n/10%10;
			int ge=n%10;
			if(n==Math.pow(ge, 3)+Math.pow(shi, 3)+Math.pow(bai, 3)){
				
				System.out.print(n+" ");
				count++;				
			}
			
			
		}
		System.out.println("三位数共有"+count+"个水仙花数");
	}

}