您的位置:首页 > 其它

Set集合解决年龄问题:年龄的4次方是个6位数。这10个数字正好包含了从0到9这10个数字,

2014-04-09 15:08 281 查看
美国数学家维纳(N.Wiener)智力早熟,11岁就上了大学。他曾在1935~1936年应邀来中国清华大学讲学。

一次,他参加某个重要会议,年轻的脸孔引人注目。于是有人询问他的年龄,他回答说:

“我年龄的立方是个4位数。我年龄的4次方是个6位数。这10个数字正好包含了从0到9这10个数字,每个都恰好出现1次。”

请你推算一下,他当时到底有多年轻。

代码:

package cn.itcast.setInterface;

import java.util.HashSet;

public class SetInterface {

//判断是否重复,加入到set集合中的元素不能重复

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++;

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐