📌  相关文章
📜  从满足给定条件的给定数组A []生成数组B []

📅  最后修改于: 2021-06-26 08:54:44             🧑  作者: Mango

给定一个由N个整数组成的数组A [] ,使得A [0] + A [1] + A [2] +…A [N – 1] = 0 。任务是为所有有效iB [0] + B [1] +生成一个数组B [] ,使B [i]⌊A[i] /2⌋⌈A[i] / 2] B [2] +…+ B [N – 1] = 0

例子:

方法:对于偶数整数,可以安全地假设B [i]将为A [i] / 2,但是对于奇数整数,要保持总和等于零,取正整数的半个ceil和正整数的下限其他半个奇数整数。由于Odd – Odd =偶数–偶数–偶数=偶数且0也是偶数,因此可以说A []总是包含偶数个奇数整数,因此总和可以为0 。因此,对于有效的输入,总会有答案。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Utility function to print
// the array elements
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Function to generate and print
// the required array
void generateArr(int arr[], int n)
{
 
    // To switch the ceil and floor
    // function alternatively
    bool flip = true;
 
    // For every element of the array
    for (int i = 0; i < n; i++) {
 
        // If the number is odd then print the ceil
        // or floor value after division by 2
        if (arr[i] & 1) {
 
            // Use the ceil and floor alternatively
            if (flip ^= true)
                cout << ceil((float)(arr[i]) / 2.0) << " ";
            else
                cout << floor((float)(arr[i]) / 2.0) << " ";
        }
 
        // If arr[i] is even then it will
        // be completely divisible by 2
        else {
            cout << arr[i] / 2 << " ";
        }
    }
}
 
// Driver code
int main()
{
    int arr[] = { 3, -5, -7, 9, 2, -2 };
    int n = sizeof(arr) / sizeof(int);
 
    generateArr(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
// Utility function to print
// the array elements
import java.util.*;
import java.lang.*;
 
class GFG
{
 
static void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        System.out.print(arr[i] + " ");
}
 
// Function to generate and print
// the required array
static void generateArr(int arr[], int n)
{
 
    // To switch the ceil and floor
    // function alternatively
    boolean flip = true;
 
    // For every element of the array
    for (int i = 0; i < n; i++)
    {
 
        // If the number is odd then print the ceil
        // or floor value after division by 2
        if ((arr[i] & 1) != 0)
        {
 
            // Use the ceil and floor alternatively
            if (flip ^= true)
                System.out.print((int)(Math.ceil(arr[i] /
                                            2.0)) + " ");
            else
                System.out.print((int)(Math.floor(arr[i] /
                                            2.0)) + " ");
        }
 
        // If arr[i] is even then it will
        // be completely divisible by 2
        else
        {
            System.out.print(arr[i] / 2 +" ");
        }
    }
}
 
// Driver code
public static void main(String []args)
{
    int arr[] = { 3, -5, -7, 9, 2, -2 };
    int n = arr.length;
 
    generateArr(arr, n);
}
}
 
// This code is contributed by Surendra_Gangwar


Python3
# Python3 implementation of the approach
from math import ceil, floor
 
# Utility function to print
# the array elements
def printArr(arr, n):
    for i in range(n):
        print(arr[i], end = " ")
 
# Function to generate and print
# the required array
def generateArr(arr, n):
 
    # To switch the ceil and floor
    # function alternatively
    flip = True
 
    # For every element of the array
    for i in range(n):
 
        # If the number is odd then print the ceil
        # or floor value after division by 2
        if (arr[i] & 1):
 
            # Use the ceil and floor alternatively
            flip ^= True
            if (flip):
                print(int(ceil((arr[i]) / 2)),
                                   end = " ")
            else:
                print(int(floor((arr[i]) / 2)),
                                    end = " ")
 
        # If arr[i] is even then it will
        # be completely divisible by 2
        else:
            print(int(arr[i] / 2), end = " ")
 
# Driver code
arr = [3, -5, -7, 9, 2, -2]
n = len(arr)
 
generateArr(arr, n)
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
// Utility function to print
// the array elements
using System;
using System.Collections.Generic;
 
class GFG
{
 
static void printArr(int []arr, int n)
{
    for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
}
 
// Function to generate and print
// the required array
static void generateArr(int []arr, int n)
{
 
    // To switch the ceil and floor
    // function alternatively
    bool flip = true;
 
    // For every element of the array
    for (int i = 0; i < n; i++)
    {
 
        // If the number is odd then print the ceil
        // or floor value after division by 2
        if ((arr[i] & 1) != 0)
        {
 
            // Use the ceil and floor alternatively
            if (flip ^= true)
                Console.Write((int)(Math.Ceiling(arr[i] /
                                            2.0)) + " ");
            else
                Console.Write((int)(Math.Floor(arr[i] /
                                            2.0)) + " ");
        }
 
        // If arr[i] is even then it will
        // be completely divisible by 2
        else
        {
            Console.Write(arr[i] / 2 +" ");
        }
    }
}
 
// Driver code
public static void Main(String []args)
{
    int []arr = { 3, -5, -7, 9, 2, -2 };
    int n = arr.Length;
 
    generateArr(arr, n);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
1 -2 -4 5 1 -1

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。