279. Perfect Squares

本文介绍了一种求解给定正整数n所需的最少完全平方数数量的方法。通过动态规划算法实现,优化了计算过程,减少了不必要的遍历。
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.

For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.

n=a+b 所以dp[n]=dp[a]+dp[b]注意其中b可以取0,然后取最小就好。直观的解法

public class Solution {
    public int numSquares(int n) {
        if(n<1) return 0;
        if(n==1) return 1;
        int[] dp = new int[n+1];
        for(int i=1; i*i <=n; i++){
            dp[i*i] = 1;
        }

        for(int i=2; i < n+1; i++){
            int t = Integer.MAX_VALUE;
            for(int j=1; j <= i/2; j++){
                t = Math.min(t, dp[j] + dp[i-j]);
            }
            dp[i] = dp[i]==1?1:t;
        }

        return dp[n];

    }
}

但是这样的复杂度还是较高。

那么关键是减少对一些不好的情况的遍历,只需考虑加和中其中一个数为平方数即可。

改进后的代码

public class Solution {
    public int numSquares(int n) {
        if(n<1) return 0;
        if(n==1) return 1;
        int[] dp = new int[n+1];
        for(int i=1; i*i <=n; i++){
            dp[i*i] = 1;
        }

        for(int i=2; i < n+1; i++){
            if(dp[i]==1) continue;
            int t = Integer.MAX_VALUE;
            for(int j=1; j*j < i; j++){
                t = Math.min(t, dp[i-j*j] +1);
            }
            dp[i] = t;
        }

        return dp[n];

    }
}
以下是满足题目要求的C语言代码: ```c #include <stdio.h> #include <math.h> int isPerfectSquare(int num) { int root = (int)sqrt(num); return root * root == num; } void findPerfectSquares() { printf("The possible perfect squares combinations are:\n"); for (int abc = 100; abc <= 999; abc++) { if (!isPerfectSquare(abc)) continue; int a = abc / 100; int b = (abc / 10) % 10; int c = abc % 10; for (int xyz = 100; xyz <= 999; xyz++) { if (!isPerfectSquare(xyz)) continue; int x = xyz / 100; int y = (xyz / 10) % 10; int z = xyz % 10; int ax = a * 10 + x; int by = b * 10 + y; int cz = c * 10 + z; if (ax >= 10 && ax <= 99 && by >= 10 && by <= 99 && cz >= 10 && cz <= 99 && isPerfectSquare(ax) && isPerfectSquare(by) && isPerfectSquare(cz)) { printf(" %d and %d\n", abc, xyz); } } } } int main() { findPerfectSquares(); return 0; } ``` --- ### 回答问题 #### 1. **实现方法**: 程序通过双重循环遍历所有可能的三位数平方数(`abc` 和 `xyz`),然后检查它们的各位组合是否可以形成三个两位数平方数(`ax`, `by`, `cz`)。如果满足条件,则输出对应的组合。 #### 2. **解释代码逻辑**: - **`isPerfectSquare` 函数**:判断一个数是否为完全平方数。通过计算其平方根并验证平方根的平方是否等于原数来实现。 - **`findPerfectSquares` 函数**: - 遍历从100到999的所有三位数,筛选出其中的完全平方数。 - 对于每个完全平方数 `abc`,提取其百位、十位和个位数字(`a`, `b`, `c`)。 - 再次遍历从100到999的所有三位数,筛选出其中的完全平方数 `xyz`。 - 提取 `xyz` 的百位、十位和个位数字(`x`, `y`, `z`)。 - 构造新的两位数 `ax`, `by`, `cz`,并检查它们是否为完全平方数且在两位数范围内(10 到 99)。 - 如果所有条件均满足,则输出 `abc` 和 `xyz` 的组合。 - **主函数**:调用 `findPerfectSquares` 函数执行核心逻辑。 #### 3. **问题产生的原因**: - 如果不正确地构造或验证 `ax`, `by`, `cz` 是否为完全平方数,则无法找到正确的组合。 - 需要确保三位数和两位数的范围限制被正确处理。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值