《水仙花数》——————打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。

mac2026-01-18  10

 

/*水仙花数是一个三位数, 例如:153是一个"水仙花数", 因为153=1的三次方+5的三次方+3的三次方。 ge = 153%10=3 shi = (153/10)%10=15%10 bai = 153/100 =1 */ class Test03 { public static void main(String[] args) { int count = 0; for(int i=100;i<1000;i++){ //求个位上的数字 int ge = i%10; //求十位上的数字 int shi = i/10%10; //求百位上的数字 int bai = i/100; if(i==ge*ge*ge+shi*shi*shi+bai*bai*bai){ count++; System.out.println(i); } } System.out.println("水仙花数一共有"+count+"个"); } }

输出结果】

最新回复(0)