📌  相关文章
📜  满足给定属性的堆叠元素的最大平方和

📅  最后修改于: 2021-04-22 03:15:34             🧑  作者: Mango

给定两个整数SN,任务是找到可以放置在堆栈中的N个整数的最大平方和,从而满足以下属性:

  • 堆栈顶部的整数不应小于紧随其后的元素。
  • 所有堆栈元素都应在[1,9]范围内。
  • 堆栈元素的总和应完全等于S。

如果无法获得这样的安排,请打印-1

例子:

方法:请按照以下步骤解决问题:

  1. 检查S是否有效,即,它是否在[N,9 * N]范围内。
  2. 初始化一个变量,例如res,以存储堆栈元素的最大平方和。
  3. 堆栈中整数的最小值可以为1,因此用1初始化所有堆栈元素。因此,从S减去N。
  4. 现在,检查值大于1的整数数目。为此,将8(如果可能)添加到从堆栈底数开始的整数,并继续将其相加直到S> 0
  5. 最后,将S%8添加到当前整数,并用1填充所有剩余的堆栈元素。
  6. 最后,添加堆栈元素的平方和。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the maximum
// sum of squares of stack elements
void maxSumOfSquares(int N,
                     int S)
{
  // Stores the sum ofsquares
  // of stack elements
  int res = 0;
 
  // Check if sum is valid
  if (S < N || S > 9 * N)
  {
    cout << (-1);
    return;
  }
 
  // Initialize all stack
  // elements with 1
  S = S - N;
 
  // Stores the count the
  // number of stack elements
  // not equal to 1
  int c = 0;
 
  // Add the sum of squares
  // of stack elements not
  // equal to 1
  while (S > 0)
  {
    c++;
    if (S / 8 > 0)
    {
      res += 9 * 9;
      S -= 8;
    }
    else
    {
      res += (S + 1) *
             (S + 1);
      break;
    }
  }
 
  // Add 1 * 1 to res as the
  // remaining stack elements
  // are 1
  res = res + (N - c);
 
  // Print the result
  cout << (res);
}
 
// Driver Code
int main()
{
  int N = 3;
  int S = 12;
 
  // Function call
  maxSumOfSquares(N, S);
}
// This code is contributed by 29AjayKumar


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the maximum
    // sum of squares of stack elements
    public static void maxSumOfSquares(
        int N, int S)
    {
        // Stores the sum ofsquares
        // of stack elements
        int res = 0;
 
        // Check if sum is valid
        if (S < N || S > 9 * N) {
 
            System.out.println(-1);
            return;
        }
 
        // Initialize all stack
        // elements with 1
        S = S - N;
 
        // Stores the count the
        // number of stack elements
        // not equal to 1
        int c = 0;
 
        // Add the sum of squares of
        // stack elements not equal to 1
        while (S > 0) {
            c++;
 
            if (S / 8 > 0) {
 
                res += 9 * 9;
                S -= 8;
            }
 
            else {
 
                res += (S + 1)
                       * (S + 1);
                break;
            }
        }
 
        // Add 1 * 1 to res as the
        // remaining stack elements are 1
        res = res + (N - c);
 
        // Print the result
        System.out.println(res);
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int N = 3;
        int S = 12;
 
        // Function call
        maxSumOfSquares(N, S);
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to find the maximum
# sum of squares of stack elements
def maxSumOfSquares(N, S):
     
    # Stores the sum ofsquares
    # of stack elements
    res = 0
 
    # Check if sum is valid
    if (S < N or S > 9 * N):
        cout << -1;
        return
 
    # Initialize all stack
    # elements with 1
    S = S - N
 
    # Stores the count the
    # number of stack elements
    # not equal to 1
    c = 0
 
    # Add the sum of squares of
    # stack elements not equal to 1
    while (S > 0):
        c += 1
 
        if (S // 8 > 0):
            res += 9 * 9
            S -= 8
        else:
            res += (S + 1) * (S + 1)
            break
 
    # Add 1 * 1 to res as the
    # remaining stack elements are 1
    res = res + (N - c)
 
    # Print the result
    print(res)
 
# Driver Code
if __name__ == '__main__':
     
    N = 3
    S = 12
 
    # Function call
    maxSumOfSquares(N, S)
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to find the maximum
// sum of squares of stack elements
public static void maxSumOfSquares(int N,
                                   int S)
{
  // Stores the sum ofsquares
  // of stack elements
  int res = 0;
 
  // Check if sum is valid
  if (S < N || S > 9 * N)
  {
    Console.WriteLine(-1);
    return;
  }
 
  // Initialize all stack
  // elements with 1
  S = S - N;
 
  // Stores the count the
  // number of stack elements
  // not equal to 1
  int c = 0;
 
  // Add the sum of squares of
  // stack elements not equal to 1
  while (S > 0)
  {
    c++;
 
    if (S / 8 > 0)
    {
      res += 9 * 9;
      S -= 8;
    }
 
    else
    {
      res += (S + 1) *
             (S + 1);
      break;
    }
  }
 
  // Add 1 * 1 to res
  // as the remaining
  // stack elements are 1
  res = res + (N - c);
 
  // Print the result
  Console.WriteLine(res);
}
 
// Driver Code
public static void Main(String []args)
{
  int N = 3;
  int S = 12;
 
  // Function call
  maxSumOfSquares(N, S);
}
}
 
// This code is contributed by shikhasingrajput


输出:
86






时间复杂度:O(N)
辅助空间: O(1)