统计水仙花数有多少个?
水仙花数: 所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。 举例:153就是一个水仙花数。 153 =1x1x1+5x5x5+3x3x3 分析: 0)定义一个统计变量 int count = 0 ; 1)水仙花:就是三位数 ---->for循环 循环中的变量为x 100 ~999 特点: 每个位的数据的立方和是当前数据本身 153 2)获取每个位上的数据 int ge= x % 10 ; int shi = x /10 % 10 ; int bai = x /10 /10 % 10 ; 3)每个位上的数据满足条件 x == ge*ge*ge+shi*shi*shi+bai*bai*bai 4)满足上面的条件 :count++ 5)for循环的输出count值 //定义统计变量 int count = 0 ; //for循环 for ( int x = 100 ; x < 1000 ; x ++ ) { //获取每个位上的数据; int ge = x % 10 ; int shi = x / 10 % 10 ; int bai = x / 10 / 10 % 10 ; //满足条件 if ( x == ( ge * ge * ge + shi * shi * shi + bai * bai * bai ) ) { //输出水仙花数 System . out .