📜  交换备用边界对

📅  最后修改于: 2021-04-23 17:33:23             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是交换第一个和最后一个元素,然后交换第三个和最后一个元素,然后交换第五个和第五个最后一个,依此类推。在所有有效操作之后打印最终数组。

方法:初始化指针i = 0j = N – 1,然后在这些指针处交换元素并更新i = i + 2j = j – 2 。当重复这些步骤。最后打印更新后的数组。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Utility function to print
// the contents of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Function to update the array
void UpdateArr(int arr[], int n)
{
 
    // Initialize the pointers
    int i = 0, j = n - 1;
 
    // While there are elements to swap
    while (i < j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
 
        // Update the pointers
        i += 2;
        j -= 2;
    }
 
    // Print the updated array
    printArr(arr, n);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    UpdateArr(arr, n);
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG
{
     
    // Utility function to print
    // the contents of an array
    static void printArr(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
     
    // Function to update the array
    static void UpdateArr(int arr[], int n)
    {
     
        // Initialize the pointers
        int i = 0, j = n - 1;
     
        // While there are elements to swap
        while (i < j)
        {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
     
            // Update the pointers
            i += 2;
            j -= 2;
        }
     
        // Print the updated array
        printArr(arr, n);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = { 1, 2, 3, 4, 5, 6 };
        int n = arr.length;
     
        UpdateArr(arr, n);
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
 
# Utility function to print
# the contents of an array
def printArr(arr, n):
 
    for i in range(n):
        print(arr[i], end = " ");
 
# Function to update the array
def UpdateArr(arr, n):
 
    # Initialize the pointers
    i = 0;
    j = n - 1;
 
    # While there are elements to swap
    while (i < j):
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
 
        # Update the pointers
        i += 2;
        j -= 2;
     
    # Print the updated array
    printArr(arr, n);
 
# Driver code
if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5, 6 ];
    n = len(arr);
 
    UpdateArr(arr, n);
 
# This code is contributed by PrinciRaj1992


C#
// C# implementation of the approach
using System;
     
class GFG
{
     
    // Utility function to print
    // the contents of an array
    static void printArr(int []arr, int n)
    {
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
     
    // Function to update the array
    static void UpdateArr(int []arr, int n)
    {
     
        // Initialize the pointers
        int i = 0, j = n - 1;
     
        // While there are elements to swap
        while (i < j)
        {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
     
            // Update the pointers
            i += 2;
            j -= 2;
        }
     
        // Print the updated array
        printArr(arr, n);
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int []arr = { 1, 2, 3, 4, 5, 6 };
        int n = arr.Length;
     
        UpdateArr(arr, n);
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
6 2 4 3 5 1