数字始终可以表示为其他数字的平方和。注意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的子问题被调用了两次。
由于再次调用了相同的子问题,因此此问题具有“重叠子问题”属性。最小平方和问题具有动态规划问题的两个属性(请参阅此内容)。像其他典型的动态编程(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
输出:
1 + 9