第六届蓝桥杯 JavaB 立方变自身
立方变自身
观察下面的现象,某个数字的立方,按位累加仍然等于自身。
1^3 = 1
8^3 = 512 5+1+2=8
17^3 = 4913 4+9+1+3=17
…
请你计算包括1,8,17在内,符合这个性质的正整数一共有多少个?
请填写该数字,不要填写任何多余的内容或说明性的文字。
答案:6
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* 因为 55~99 的立方是6位数,假如这六位数都是9加一起才是54,所以55-90之间不可能有符合条件的!
*
* @description TODO
* @author frontier
* @time 2019年3月12日 上午11:20:25
*
*/
public class 结果填空2立方变自身 {
static int n;
static int n3;
static int count;
static int sum;
public static void main(String[] args) throws FileNotFoundException {
long time = System.currentTimeMillis();
Scanner in = new Scanner(new File("src/JavaB/s7/8.txt"));
for (int i = 1; i <= 100000; ++i) {
if (check(i)) {
count++;
System.out.println(i);
}
}
System.out.println("一共个数: " + count);
System.out.println((System.currentTimeMillis() - time) + "ms");
}
static boolean check(int n) {
n3 = (int) Math.pow(n, 3);
String s = n3 + "";
sum = 0;
for (int i = 0; i < s.length(); ++i)
sum += Integer.parseInt(s.charAt(i) + "");
if (sum == n)
return true;
return false;
}
}