java实现第六届蓝桥杯立方体自身

立方变自身

题目描述
观察下面的现象,某个数字的立方,按位累加仍然等于自身。
1^3 = 1
8^3 = 512 5+1+2=8
17^3 = 4913 4+9+1+3=17

请你计算包括1,8,17在内,符合这个性质的正整数一共有多少个?

请填写该数字,不要填写任何多余的内容或说明性的文字。

结果:6

public class Main {
    
    //暴力枚举,轻易可知,当n > 100时,一定没有符合题意正整数,原因:100^3共7位置,7*9<100,依次类推
    
    
    public static void main(String[] args) {
        int count = 0;
        for(long i = 1;i <= 100000;i++) {  //此处使用较大数据检测猜测
            long temp = i * i * i;
            long temp1 = 0;
            while(temp > 0) {
                temp1 += (temp % 10);
                temp = temp / 10;
            }
            if(i == temp1) {
                System.out.println("i = "+i+", i^3 = "+(i*i*i));
                count++;
            }
        }
        System.out.println(count);
    }
}
原文地址:https://www.cnblogs.com/a1439775520/p/12947604.html