请你推算一下,他当时到底有多年轻。
/** * 标题: 猜年龄 * 美国数学家维纳(N.Wiener)智力早熟,11岁就上了大学。 * 他曾在1935~1936年应邀来中国清华大学讲学。 * 一次,他参加某个重要会议,年轻的脸孔引人注目。 * 于是有人询问他的年龄,他回答说: * “我年龄的立方是个4位数。我年龄的4次方是个6位数。 * 这10个数字正好包含了从0到9这10个数字,每个都恰好出现1次。” * 请你推算一下,他当时到底有多年轻。 */ import java.util.HashSet; public class SetTest { // 是否包含重复的 public static boolean isRepeat(String num) { HashSet<Character> set = new HashSet<Character>(); for (int i = 0; i < num.length(); i++) { if (!set.add(num.charAt(i))) { return false; } } return true; } public static void main(String[] args) { int age = 1; while (true) { String num1 = String.valueOf((int) Math.pow(age, 3)); String num2 = String.valueOf((int) Math.pow(age, 4)); if (num1.length() == 4 && num2.length() == 6) { if (isRepeat(num1 + num2)) { System.out.println("年龄为:" + age); System.out.println("立方为:" + num1); System.out.println("四次方为:" + num2); break; } } else { age++; } } } }
Conclusion 年龄为:18 立方为:5832 四次方为:104976
原文:http://blog.csdn.net/u011925500/article/details/19284537