📜  通过在每个元素上使用 ceil 或 floor 使数组总和为 0

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

通过在每个元素上使用 ceil 或 floor 使数组总和为 0

给定一个由N个浮点整数组成的数组arr[] ,任务是通过对每个数组元素执行 ceil() 或 floor() 来修改数组,使数组元素的总和接近0

例子:

方法:给定的问题可以通过找到所有数组元素的 ceil() 值的总和来解决,如果数组的总和为正,则找到该元素数量的 ceil,使得 ceil 变得最接近于值 0。以下步骤可解决给定问题:

  • 初始化一个变量,比如sum0 ,它存储数组元素的总和。
  • 初始化一个数组,比如A[] ,它存储更新的数组元素。
  • 遍历数组arr[]并找到数组元素的ceil()和并将A[i]的值更新为值ceil(arr[i])
  • 如果sum的值为正,则找到某个数组元素的下限以将和减少到最接近0 ,并且此类数组元素的计数由sumN的最小值给出。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to modify the array element
// such that the sum is close to 0
void setSumtoZero(double arr[], int N)
{
    // Stores the modified elements
    int A[N];
 
    // Stores the sum of array element
    int sum = 0;
 
    // Stores minimum size of the array
    int m = INT_MIN;
 
    // Traverse the array and find the
    // sum
    for (int i = 0; i < N; i++) {
        sum += ceil(arr[i]);
        A[i] = ceil(arr[i]);
    }
 
    // If sum is positive
    if (sum > 0) {
 
        // Find the minimum number of
        // elements that must be changed
        m = min(sum, N);
 
        // Iterate until M elements are
        // modified or the array end
        for (int i = 0; i < N && m > 0; i++) {
 
            // Update the current array
            // elements to its floor
            A[i] = floor(arr[i]);
            if (A[i] != floor(arr[i]))
                m--;
        }
    }
 
    // Print the resultant array
    for (int i = 0; i < N; i++) {
        cout << A[i] << " ";
    }
}
 
// Driver Code
int main()
{
    double arr[] = { -2, -2, 4.5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    setSumtoZero(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to modify the array element
// such that the sum is close to 0
static void setSumtoZero(double arr[], int N)
{
   
    // Stores the modified elements
    int []A = new int[N];
 
    // Stores the sum of array element
    int sum = 0;
 
    // Stores minimum size of the array
    int m = Integer.MIN_VALUE;
 
    // Traverse the array and find the
    // sum
    for (int i = 0; i < N; i++) {
        sum += Math.ceil(arr[i]);
        A[i] = (int) Math.ceil(arr[i]);
    }
 
    // If sum is positive
    if (sum > 0) {
 
        // Find the minimum number of
        // elements that must be changed
        m = Math.min(sum, N);
 
        // Iterate until M elements are
        // modified or the array end
        for (int i = 0; i < N && m > 0; i++) {
 
            // Update the current array
            // elements to its floor
            A[i] = (int) Math.floor(arr[i]);
            if (A[i] != Math.floor(arr[i]))
                m--;
        }
    }
 
    // Print the resultant array
    for (int i = 0; i < N; i++) {
        System.out.print(A[i]+ " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    double arr[] = { -2, -2, 4.5 };
    int N = arr.length;
    setSumtoZero(arr, N);
 
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python 3 program for the above approach
import sys
from math import ceil,floor
 
# Function to modify the array element
# such that the sum is close to 0
def setSumtoZero(arr, N):
   
    # Stores the modified elements
    A = [0 for i in range(N)]
 
    # Stores the sum of array element
    sum = 0
 
    # Stores minimum size of the array
    m = -sys.maxsize-1
 
    # Traverse the array and find the
    # sum
    for i in range(N):
        sum += ceil(arr[i])
        A[i] = ceil(arr[i])
 
    # If sum is positive
    if (sum > 0):
 
        # Find the minimum number of
        # elements that must be changed
        m = min(sum, N)
 
        # Iterate until M elements are
        # modified or the array end
        i = 0
        while(i < N and m > 0):
           
            # Update the current array
            # elements to its floor
            A[i] = floor(arr[i])
            if (A[i] != floor(arr[i])):
                m -= 1
            i += 1
 
    # Print the resultant array
    for i in range(N):
        print(A[i],end = " ")
 
# Driver Code
if __name__ == '__main__':
    arr = [-2, -2, 4.5]
    N = len(arr)
    setSumtoZero(arr, N)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
public class GFG{
 
// Function to modify the array element
// such that the sum is close to 0
static void setSumtoZero(double []arr, int N)
{
   
    // Stores the modified elements
    int []A = new int[N];
 
    // Stores the sum of array element
    int sum = 0;
 
    // Stores minimum size of the array
    int m = int.MinValue;
 
    // Traverse the array and find the
    // sum
    for (int i = 0; i < N; i++) {
        sum += (int)Math.Ceiling(arr[i]);
        A[i] = (int) Math.Ceiling(arr[i]);
    }
 
    // If sum is positive
    if (sum > 0) {
 
        // Find the minimum number of
        // elements that must be changed
        m = Math.Min(sum, N);
 
        // Iterate until M elements are
        // modified or the array end
        for (int i = 0; i < N && m > 0; i++) {
 
            // Update the current array
            // elements to its floor
            A[i] = (int) Math.Floor(arr[i]);
            if (A[i] != Math.Floor(arr[i]))
                m--;
        }
    }
 
    // Print the resultant array
    for (int i = 0; i < N; i++) {
        Console.Write(A[i]+ " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    double []arr = { -2, -2, 4.5 };
    int N = arr.Length;
    setSumtoZero(arr, N);
 
}
}
 
  
 
// This code is contributed by 29AjayKumar


Javascript



输出:
-2 -2 4

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