一个数总是可以表示为其他数的平方和。请注意, 1 是一个正方形,我们总是可以将数字分解为(1*1 + 1*1 + 1*1 + …) 。给定一个数字N ,任务是将N表示为最小平方数的总和。
例子:
Input : 10
Output : 1 + 9
These are all possible ways
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1 + 4
1 + 1 + 4 + 4
1 + 9
Choose one with minimum numbers
Input : 25
Output : 25
先决条件:总和等于给定数字 N 的最小平方数
方法:这是动态规划的典型应用。当我们从 N = 6 开始时,我们可以通过减去 1 的平方,即 1,4 次,以及减去 2 的平方,即 4,1 次来达到 2。所以 2 的子问题被调用了两次。
由于再次调用相同的子问题,此问题具有重叠子问题的属性。所以最小平方和问题具有动态规划问题的两个属性(见this和this)。与其他典型的动态规划 (DP) 问题一样,通过以自底向上的方式构造临时数组table[][]可以避免相同子问题的重新计算。
下面是上述方法的实现:
C++
// C++ program to represent N as the
// sum of minimum square numbers.
#include
using namespace std;
// Function for finding
// minimum square numbers
vector minSqrNum(int n)
{
// A[i] of array arr store
// minimum count of
// square number to get i
int arr[n + 1], k;
// sqrNum[i] store last
// square number to get i
int sqrNum[n + 1];
vector v;
// Initialize
arr[0] = 0;
sqrNum[0] = 0;
// Find minimun count of
// square number for
// all value 1 to n
for (int i = 1; i <= n; i++)
{
// In worst case it will
// be arr[i-1]+1 we use all
// combination of a[i-1] and add 1
arr[i] = arr[i - 1] + 1;
sqrNum[i] = 1;
k = 1;
// Check for all square
// number less or equal to i
while (k * k <= i)
{
// if it gives less
// count then update it
if (arr[i] > arr[i - k * k] + 1)
{
arr[i] = arr[i - k * k] + 1;
sqrNum[i] = k * k;
}
k++;
}
}
// Vector v stores optimum
// square number whose sum give N
while (n > 0)
{
v.push_back(sqrNum[n]);
n -= sqrNum[n];
}
return v;
}
// Driver code
int main()
{
int n = 10;
vector v;
// Calling funcion
v = minSqrNum(n);
// Printing vector
for (auto i = v.begin();
i != v.end(); i++)
{
cout << *i;
if (i + 1 != v.end())
cout << " + ";
}
return 0;
}
Java
// Java program to represent
// N as the sum of minimum
// square numbers.
import java.util.*;
class GFG{
// Function for finding
// minimum square numbers
static Vector minSqrNum(int n)
{
// A[i] of array arr store
// minimum count of
// square number to get i
int []arr = new int[n + 1];
int k = 0;
// sqrNum[i] store last
// square number to get i
int []sqrNum = new int[n + 1];
Vector v = new Vector<>();
// Initialize
arr[0] = 0;
sqrNum[0] = 0;
// Find minimun count of
// square number for
// all value 1 to n
for (int i = 1; i <= n; i++)
{
// In worst case it will
// be arr[i-1]+1 we use all
// combination of a[i-1] and add 1
arr[i] = arr[i - 1] + 1;
sqrNum[i] = 1;
k = 1;
// Check for all square
// number less or equal to i
while (k * k <= i)
{
// if it gives less
// count then update it
if (arr[i] > arr[i - k * k] + 1)
{
arr[i] = arr[i - k * k] + 1;
sqrNum[i] = k * k;
}
k++;
}
}
// Vector v stores optimum
// square number whose sum give N
while (n > 0)
{
v.add(sqrNum[n]);
n -= sqrNum[n];
}
return v;
}
// Driver code
public static void main(String[] args)
{
int n = 10;
Vector v;
// Calling funcion
v = minSqrNum(n);
// Printing vector
for (int i = 0; i
Python3
# Python3 program to represent N as the
# sum of minimum square numbers.
# Function for finding
# minimum square numbers
def minSqrNum(n):
# arr[i] of array arr store
# minimum count of
# square number to get i
arr = [0] * (n + 1)
# sqrNum[i] store last
# square number to get i
sqrNum = [0] * (n + 1)
v = []
# Find minimun count of
# square number for
# all value 1 to n
for i in range(n + 1):
# In worst case it will
# be arr[i-1]+1 we use all
# combination of a[i-1] and add 1
arr[i] = arr[i - 1] + 1
sqrNum[i] = 1
k = 1;
# Check for all square
# number less or equal to i
while (k * k <= i):
# If it gives less
# count then update it
if (arr[i] > arr[i - k * k] + 1):
arr[i] = arr[i - k * k] + 1
sqrNum[i] = k * k
k += 1
# v stores optimum
# square number whose sum give N
while (n > 0):
v.append(sqrNum[n])
n -= sqrNum[n];
return v
# Driver code
n = 10
# Calling funcion
v = minSqrNum(n)
# Printing vector
for i in range(len(v)):
print(v[i], end = "")
if (i < len(v) - 1):
print(" + ", end = "")
# This article is contributed by Apurvaraj
C#
// C# program to represent
// N as the sum of minimum
// square numbers.
using System;
using System.Collections.Generic;
class GFG{
// Function for finding
// minimum square numbers
static List minSqrNum(int n)
{
// A[i] of array arr store
// minimum count of
// square number to get i
int []arr = new int[n + 1];
int k = 0;
// sqrNum[i] store last
// square number to get i
int []sqrNum = new int[n + 1];
List v = new List();
// Initialize
arr[0] = 0;
sqrNum[0] = 0;
// Find minimun count of
// square number for
// all value 1 to n
for (int i = 1; i <= n; i++)
{
// In worst case it will
// be arr[i-1]+1 we use all
// combination of a[i-1] and add 1
arr[i] = arr[i - 1] + 1;
sqrNum[i] = 1;
k = 1;
// Check for all square
// number less or equal to i
while (k * k <= i)
{
// if it gives less
// count then update it
if (arr[i] > arr[i - k * k] + 1)
{
arr[i] = arr[i - k * k] + 1;
sqrNum[i] = k * k;
}
k++;
}
}
// List v stores optimum
// square number whose sum give N
while (n > 0)
{
v.Add(sqrNum[n]);
n -= sqrNum[n];
}
return v;
}
// Driver code
public static void Main(String[] args)
{
int n = 10;
List v;
// Calling funcion
v = minSqrNum(n);
// Printing vector
for (int i = 0; i
Javascript
输出:
1 + 9
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。