立方變自身--枚舉

統計一個數的三次方得到的數後將各位加起來的到原來的數

package test;
public class main {
    public static void main(String[] args) {
        int x = 0;
        for(int i = 1 ; i < 1000000; i++) {         //三次方太過大了 採用暴力 不過要注意記得檢驗 要除去0
            if(is_true(i))
                x++;
        }
        System.out.println(x);
    }
    public static boolean is_true(int i ) {
        int sum = i*i*i;
        String str = sum + "";
        int count = 0 ;
        for(int x = 0 ; x < str.length() ; x++) {
            count += str.charAt(x)-'0';
        }
        if(count == i )
            return true;
        return false;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章