Ural1073-Square Country

给定一个数n,求这个数最少由几个完全平方数相加得到。
状态转移方程:dp[i] = ∑dp[i-j*j]+1.

#include 
#include 

const int maxn = 60000 + 5;
const int inf = maxn;

int dp[maxn];

int main(int argc, char const *argv[]) {
    int n;
    scanf("%d", &n);
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
        dp[i] = inf;
        for (int j = 0; j * j <= i; j++) {
            dp[i] = std::min(dp[i], dp[i-j*j] + 1);
        }
    }
    printf("%d\n", dp[n]);
    return 0;
}

你可能感兴趣的:(Ural,动态规划DP)