📜  自动缩放后在时间范围结束时查找实例

📅  最后修改于: 2022-05-13 01:56:05.800000             🧑  作者: Mango

自动缩放后在时间范围结束时查找实例

给定一个整数、 instances和一个大小为N的数组arr[] ,表示每秒计算系统的平均利用率百分比,任务是找到时间帧结束时的实例数,使得计算系统根据以下规则自动缩放实例数:

例子:

方法:可以通过遍历给定数组arr[]来解决给定的问题,如果当前元素小于 25,则如果实例数大于 1,则将实例数除以 2。否则,如果当前值为大于 60 如果实例数不大于 10 8 ,则将实例数乘以 2 ,在执行这两个操作中的任何一个后,将当前索引增加 10。按照以下步骤解决问题:

  • 使用变量i遍历数组arr[]并执行以下步骤:
    • 如果arr[i]小于 25 并且实例大于 1,则将实例除以 2 并将i增加10
    • 如果arr[i]大于 60 并且实例小于或等于 10 8 ,则将实例乘以 2 并将i增加10
    • 将索引i增加1
  • 完成上述步骤后,打印实例数作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the number of
// instances after compintion
void finalInstances(int instances,
                                  int arr[], int N)
{
    int i = 0;
 
    // Traverse the array, arr[]
    while (i < N)
    {
         
        // If current element is less
        // than 25
        if (arr[i] < 25 && instances > 1)
        {
             
            // Divide instances by 2 and take ceil value
            double temp = (instances / 2.0);
            instances = (int)(ceil(temp));
            i = i + 10;
        }
 
        // If the current element is
        // greater than 60
        else if (arr[i] > 60 &&
                 instances <= (2*pow(10, 8)))
        {
             
            // Double the instances
            instances = instances * 2;
            i = i + 10;
        }
        i = i + 1;
    }
     
    // Print the instances at the end
    // of the traversal
    cout << instances;
}
 
// Driver Code
int main()
{
    int instances = 2;
    int arr[] = { 25, 23, 1, 2, 3, 4, 5,
                  6, 7, 8, 9, 10, 76, 80 };
                   
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    finalInstances(instances, arr, N);
}
 
// This code is contributed by splevel62


Java
// Java program for above approach
class GFG{
 
// Function to find the number of
// instances after compintion
public static void finalInstances(int instances,
                                  int[] arr)
{
    int i = 0;
 
    // Traverse the array, arr[]
    while (i < arr.length)
    {
         
        // If current element is less
        // than 25
        if (arr[i] < 25 && instances > 1)
        {
             
            // Divide instances by 2
            instances = (instances / 2);
            i = i + 10;
        }
 
        // If the current element is
        // greater than 60
        else if (arr[i] > 60 &&
                 instances <= Math.pow(10, 8))
        {
             
            // Double the instances
            instances = instances * 2;
            i = i + 10;
        }
        i = i + 1;
    }
     
    // Print the instances at the end
    // of the traversal
    System.out.println(instances);
}
 
// Driver Code
public static void main(String args[])
{
    int instances = 2;
    int[] arr = { 25, 23, 1, 2, 3, 4, 5,
                  6, 7, 8, 9, 10, 76, 80 };
 
    // Function Call
    finalInstances(instances, arr);
}
}
 
// This code is contributed by _saurabh_jaiswal


Python3
# Python program for the above approach
from math import ceil
 
# Function to find the number of
# instances after completion
def finalInstances(instances, arr):
    i = 0
 
    # Traverse the array, arr[]
    while i < len(arr):
 
        # If current element is less
        # than 25
        if arr[i] < 25 and instances > 1:
           
            # Divide instances by 2
            instances = ceil(instances / 2)
            i += 10
 
        # If the current element is
        # greater than 60
        elif arr[i] > 60 and instances <= 10**8:
           
            # Double the instances
            instances *= 2
            i += 10
        i += 1
 
    # Print the instances at the end
    # of the traversal
    print(instances)
 
# Driver Code
instances = 2
 
arr = [25, 23, 1, 2, 3, 4, 5,
       6, 7, 8, 9, 10, 76, 80]
 
# Function Call
finalInstances(instances, arr)


C#
// C# program for above approach
using System;
 
class GFG{
 
// Function to find the number of
// instances after compintion
static void finalInstances(int instances, int[] arr)
{
        int i = 0;
   
        // Traverse the array, arr[]
        while (i < arr.Length) {
 
            // If current element is less
            // than 25
            if (arr[i] < 25 && instances > 1) {
 
                // Divide instances by 2
                instances = (instances / 2);
                i = i + 10;
            }
 
            // If the current element is
            // greater than 60
            else if (arr[i] > 60 && instances <= Math.Pow(10, 8))
            {
 
                // Double the instances
                instances = instances * 2;
                i = i + 10;
 
            }
            i = i + 1;
 
        }
        // Print the instances at the end
        // of the traversal
        Console.Write(instances);
    }
 
 
// Driver Code
static void Main()
{
    int instances = 2;
 
    int[] arr = {25, 23, 1, 2, 3, 4, 5,
        6, 7, 8, 9, 10, 76, 80};
 
    // Function Call
    finalInstances(instances, arr);
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
2

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