You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class33;
public class Problem_0279_PerfectSquares {
// 暴力解
public static int numSquares1(int n) {
int res = n, num = 2;
while (num * num <= n) {
int a = n / (num * num), b = n % (num * num);
res = Math.min(res, a + numSquares1(b));
num++;
}
return res;
}
// 1 : 1, 4, 9, 16, 25, 36, ...
// 4 : 7, 15, 23, 28, 31, 39, 47, 55, 60, 63, 71, ...
// 规律解
// 规律一个数不超过4
// 规律二出现1个的时候显而易见
// 规律三:任何数 % 8 == 7一定是4个
// 规律四任何数消去4的因子之后剩下restrest % 8 == 7一定是4个
public static int numSquares2(int n) {
int rest = n;
while (rest % 4 == 0) {
rest /= 4;
}
if (rest % 8 == 7) {
return 4;
}
int f = (int) Math.sqrt(n);
if (f * f == n) {
return 1;
}
for (int first = 1; first * first <= n; first++) {
int second = (int) Math.sqrt(n - first * first);
if (first * first + second * second == n) {
return 2;
}
}
return 3;
}
// 数学解
// 1四平方和定理
// 2任何数消掉4的因子结论不变
public static int numSquares3(int n) {
while (n % 4 == 0) {
n /= 4;
}
if (n % 8 == 7) {
return 4;
}
for (int a = 0; a * a <= n; ++a) {
// a * a + b * b = n
int b = (int) Math.sqrt(n - a * a);
if (a * a + b * b == n) {
return (a > 0 && b > 0) ? 2 : 1;
}
}
return 3;
}
public static void main(String[] args) {
for (int i = 1; i < 1000; i++) {
System.out.println(i + " , " + numSquares1(i));
}
}
}