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

📅  最后修改于: 2021-09-07 02:24:22             🧑  作者: 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


Javascript


输出:
86

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