黑马程序员技术交流社区

标题: 求水仙花数(笔试题) [打印本页]

作者: 唯我独赞mo    时间: 2015-11-14 23:48
标题: 求水仙花数(笔试题)
题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。并统计水仙花个数
代码如下:
  1. public static void main(String[] args) {
  2.                 //for版本
  3.                 int count1 = 0;
  4.                   for(int x = 100;x<=1000;x++){
  5.                           int ge = x%10;
  6.                                 int shi = x/10%10;
  7.                                 int bai = x/10/10%10;
  8.                                 if(ge*ge*ge+shi*shi*shi+bai*bai*bai == x){
  9.                                         System.out.println(x);
  10.                                         count1++;
  11.                                 }
  12.                   }
  13.                   System.out.println("水仙花的个数为:"+count1+"个");
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2