黑马程序员技术交流社区
标题:
求大神指点
[打印本页]
作者:
天笑
时间:
2015-10-5 11:39
标题:
求大神指点
一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
作者:
313098819
时间:
2015-10-14 21:15
用数学方法能,但是代码我不会这么去实现.
作者:
往事如风555
时间:
2015-10-17 23:01
代码如下,仅供参考
public class Demo7 {
public static void main(String[] args) {
int x = 1;
while (true) {
double n = Math.sqrt(x + 268) - Math.sqrt(x + 100);
if (Math.floor(n) != n) {
x++;
}
else {
System.out.println(x);
break;
}
}
}
}
作者:
yuanjun52306
时间:
2015-10-18 00:32
完全平方数就是1*1,2*2,3*3这样的数,开方是两个一样的非负整数
作者:
yuanjun52306
时间:
2015-10-18 00:33
Math.sqrt()这个方法是求开方。好了,我科普完了。。
作者:
yuanjun52306
时间:
2015-10-18 01:19
public class Demo7 {
public static void main(String[] args) {
double x = 1;
while(true){
double y = Math.sqrt(x+100);
//先判断x+100的平方根y是不是整数,是的话再判断x+100是不是完全平方数
if(isInt(y) && y*y == x+100){
double z = Math.sqrt(x+268);
//还是先判断x+268的平方根z是不是整数,是的话再判断x+268是不是完全平方数
if(isInt(z) && z*z == x+268){
System.out.println(x);
break;
}else{
x++;
}
}else{
x++;
}
}
}
//判断是否整数
public static boolean isInt(double d)
{
Double db = new Double(d);
return db.intValue() == d;
}
}
复制代码
作者:
往事如风555
时间:
2015-10-18 21:49
按我的代码运行出来是21,完全没有问题啊。
21+100=11*11;
21+268 = 17*17;
不用那么多判断吧
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2