📜  给定时间内可以装满的容器数量

📅  最后修改于: 2021-04-21 22:00:02             🧑  作者: Mango

给定数字N和时间X单位,任务是找到如下图所示,如果容器以金字塔形式排列,则完全填充为X单位的容器数。
注意:下面的示例是N = 3的金字塔形排列,其中N表示容器的金字塔形排列中的级别数,这样,级别1具有1个容器,级别2具有2个容器,直到N级。始终将液体倒入第一层的最上层容器中。当一个高度的容器从其两侧溢出时,将填充较低高度的容器。每秒注入的液体量等于容器的体积。

例子:

方法:使用贪婪方法可以解决此问题。步骤如下:

  • 容器装满后,液体从容器的两侧均匀地流入并填充下方的容器。
  • 将此容器的吡喃酰胺视为基质。因此, cont [i] [j]ij容器中的液体。
  • 因此,当发生溢出时,液体流向cont [i + 1] [j]cont [i + 1] [j + 1]
  • 由于将液体倒入X秒钟,因此倒入的液体总量为X单位。
  • 因此,可以倒入容器的最大值可以是X单位,可以倒入以完全填充容器的最小值是1个单位。

由于总是将液体倒入最上面的容器中,因此让最上面的容器具有最大值,即X单位。
该算法的步骤如下:

  1. 对于每个级别的n个容器,从1到N启动外部循环。在此循环内,为每行的每个容器从1到i开始另一个循环。还声明一个计数器count = 0 ,用于对要装满的容器进行计数。
  2. 如果cont [i] [j]的值大于或等于1 (表示已填充),则将count递增1 ,然后为cont [i + 1] [j]g [i + 1] ] [j + 1]倒入液体,其值分别增加(g [i] [j] -1)/ 2的值,这是因为液体在两者中均被分成了一半。
  3. 这样,继续循环,并为每个已填充的容器增加计数。循环结束时,我们的计数将是必需的答案。

下面是上述方法的实现:

C++
// C++ program for the above problem
#include 
using namespace std;
 
// matrix of containers
double cont[1000][1000];
 
// function to find the number
// of containers that will be
// filled in X seconds
void num_of_containers(int n,
                       double x)
{
    int count = 0;
 
    // container on top level
    cont[1][1] = x;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
 
            // if container gets filled
            if (cont[i][j] >= (double)1) {
                count++;
 
                // dividing the liquid
                // equally in two halves
                cont[i + 1][j]
                    += (cont[i][j]
                        - (double)1)
                       / (double)2;
 
                cont[i + 1][j + 1]
                    += (cont[i][j]
                        - (double)1)
                       / (double)2;
            }
        }
    }
    cout << count;
}
 
// driver code
int main()
{
    int n = 3;
    double x = 5;
    num_of_containers(n, x);
    return 0;
}


Java
// Java program for the above problem
import java.util.*;
 
class GFG{
     
// Matrix of containers
static double cont[][] = new double[1000][1000];
 
// Function to find the number
// of containers that will be
// filled in X seconds
static void num_of_containers(int n, double x)
{
    int count = 0;
 
    // Container on top level
    cont[1][1] = x;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= i; j++)
        {
 
            // If container gets filled
            if (cont[i][j] >= (double)1)
            {
                count++;
 
                // Dividing the liquid
                // equally in two halves
                cont[i + 1][j] += (cont[i][j] -
                                   (double)1) /
                                   (double)2;
 
                cont[i + 1][j + 1] += (cont[i][j] -
                                       (double)1) /
                                       (double)2;
            }
        }
    }
    System.out.print(count);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
    double x = 5;
     
    num_of_containers(n, x);
}
}
 
// This code is contributed by jrishabh99


Python3
# Python3 program for the above problem
 
# Matrix of containers
cont = [[ 0 for i in range(1000)]
            for j in range(1000)]
             
# Function to find the number          
# of containers that will be          
# filled in X seconds          
def num_of_containers(n, x):         
     
    count = 0    
     
    # Container on top level          
    cont[1][1] = x    
     
    for i in range(1, n + 1):
        for j in range(1, i + 1):
             
             # If container gets filled          
             if (cont[i][j] >= 1):         
                count += 1         
                 
                # Dividing the liquid          
                # equally in two halves          
                cont[i + 1][j] += (cont[i][j] - 1) / 2         
                cont[i + 1][j + 1] += (cont[i][j] - 1) / 2
                 
    print(count)
     
# Driver code          
n = 3         
x = 5   
 
num_of_containers(n, x)
 
# This code is contributed by yatinagg


C#
// C# program for the above problem
using System;
 
class GFG{
     
// Matrix of containers
static double [,]cont = new double[1000, 1000];
 
// Function to find the number
// of containers that will be
// filled in X seconds
static void num_of_containers(int n, double x)
{
    int count = 0;
 
    // Container on top level
    cont[1, 1] = x;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= i; j++)
        {
             
            // If container gets filled
            if (cont[i, j] >= (double)1)
            {
                count++;
 
                // Dividing the liquid
                // equally in two halves
                cont[i + 1, j] += (cont[i, j] -
                                  (double)1) /
                                  (double)2;
 
                cont[i + 1, j + 1] += (cont[i, j] -
                                      (double)1) /
                                      (double)2;
            }
        }
    }
    Console.Write(count);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
    double x = 5;
     
    num_of_containers(n, x);
}
}
 
// This code is contributed by Princi Singh


Javascript


输出
4

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