1 package test_2_3; 2 3 public class PerfectSquare { 4 5 public static void main(String[] args) { 6 /** 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? */ 7 8 getNum(); 9 10 } 11 12 private static void getNum() { 13 int x = 1; 14 while (true) { 15 int y = x * x; 16 int z = 1; 17 while (true) { 18 if (z * z - y >= 168) { 19 break; 20 } 21 z++; 22 } 23 24 if (z * z - x * x == 168 && (z * z - 168 - 100) >= 0) { 25 System.out.println("该数为:" + (z * z - 168 - 100)); 26 break; 27 } else { 28 x++; 29 } 30 31 } 32 } 33 34 }
结果如下:
该数为:21
[20-04-27][Self-test 12]Java PerfectSquare
原文:https://www.cnblogs.com/mirai3usi9/p/12786504.html