每日算法之立方变自身

观察下面的现象,某个数字的立方,按位累加仍然等于自身。
1^3 = 1 
8^3 = 512 5+1+2=8
17^3 = 4913 4+9+1+3=17
...
请你计算包括1,8,17在内,符合这个性质内的正整数一共有多少个?
请填写该数字,不要填写任何多余的内容或说明性的文字。

 

由于不知道会有几位数的出现,所以递归的使用很关键

package Cube;
public class Main {
    static  int count = 0;
    public  static  void main(String  args[]){
        int i;
        for(i = 1;i<100;i++){
            int s=i*i*i;
            if(i ==fun(s)){
                System.out.println(i+"  ");
                ++count;
            }
        }
        System.out.print(count);

    }
    public  static  int fun(int a){
        if(a<10){
            return  a;
        }
        else
            return fun(a/10)+a%10;
    }
}

  

posted @ 2018-04-22 19:59  怡城  阅读(199)  评论(0编辑  收藏  举报