- package test;
- /*
- * 需求: 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
- * 例如: 153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
- *
- * 思路:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
- * 设百位a,十位b,个位c 三个数为int
- * a = i/100;
- * b = i/10 - a*10;
- * c = i - b*10 - a*100
- */
- public class Test3 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int a = 0; //百位
- int b = 0; //十位
- int c = 0; //个位
- for(int i=100;i<=999;i++)
- {
- a = i/100;
- b = i/10 - a*10;
- c = i - b*10 - a*100;
- if(i==(Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3)))
- {
- System.out.print(i + " ,");
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1802201614562.html
来源: http://www.codesnippet.cn/detail/1802201614562.html