黑马程序员技术交流社区
标题:
求水仙花数(笔试题)
[打印本页]
作者:
唯我独赞mo
时间:
2015-11-14 23:48
标题:
求水仙花数(笔试题)
题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。并统计水仙花个数
代码如下:
public static void main(String[] args) {
//for版本
int count1 = 0;
for(int x = 100;x<=1000;x++){
int ge = x%10;
int shi = x/10%10;
int bai = x/10/10%10;
if(ge*ge*ge+shi*shi*shi+bai*bai*bai == x){
System.out.println(x);
count1++;
}
}
System.out.println("水仙花的个数为:"+count1+"个");
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2