📌  相关文章
📜  程序以相反的顺序将一个数组的内容复制到另一个数组中

📅  最后修改于: 2021-05-25 18:33:39             🧑  作者: Mango

给定一个数组,任务是将这些数组元素复制到反向数组中的另一个数组中。
例子:

Input: array: 1 2 3 4 5 
Output: 5 4 3 2 1 

Input: array: 10 20 30 40 50 
Output: 50 40 30 20 10  

len为原始数组的长度。我们将每个元素original_arr [i]复制到copy_arr [ni-1]以在copy_arr []中获得反向。

C++
// C program to copy the contents
// of one array into another
// in the reverse order
 
#include 
 
// Function to print the array
void printArray(int arr[], int len)
{
    int i;
    for (i = 0; i < len; i++) {
        printf("%d ", arr[i]);
    }
}
 
// Driver code
int main()
{
    int original_arr[] = {1, 2, 3, 4, 5};
    int len = sizeof(original_arr)/sizeof(original_arr[0]);
 
    int copied_arr[len], i, j;
    
    // Copy the elements of the array
    // in the copied_arr in Reverse Order
    for (i = 0; i < len; i++) {
        copied_arr[i] = original_arr[len - i - 1];
    }
 
    // Print the original_arr
    printf("\nOriginal array: ");
    printArray(original_arr, len);
 
    // Print the copied array
    printf("\nResultant array: ");
    printArray(copied_arr, len);
 
    return 0;
}


Java
// Java program to copy the contents
// of one array into another
// in the reverse order
class GFG {
 
// Function to print the array
static void printArray(int arr[], int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        System.out.printf("%d ", arr[i]);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int original_arr[] = {1, 2, 3, 4, 5};
    int len = original_arr.length;
    int copied_arr[] = new int[len], i, j;
     
    // Copy the elements of the array
    // in the copied_arr in Reverse Order
    for (i = 0; i < len; i++)
    {
        copied_arr[i] = original_arr[len - i - 1];
    }
 
    // Print the original_arr
    System.out.printf("\nOriginal array: ");
    printArray(original_arr, len);
 
    // Print the copied array
    System.out.printf("\nResultant array: ");
    printArray(copied_arr, len);
    }
}
 
// This code is contributed by
// PrinciRaj1992


Python3
# Python3 program to copy the contents of one
# array into another in the reverse order
import math as mt
 
# Function to print the array
def printArray(arr, Len):
 
    for i in range(Len):
        print(arr[i], end = " ")
     
# Driver code
original_arr = [1, 2, 3, 4, 5]
Len = len(original_arr)
 
copied_arr = [0 for i in range(Len)]
 
# Copy the elements of the array
# in the copied_arr in Reverse Order
for i in range(Len):
    copied_arr[i] = original_arr[Len - i - 1]
 
# Print the original_arr
print("Original array: ", end = "")
printArray(original_arr, Len)
 
# Print the copied array
print("\nResultant array: ", end = "")
printArray(copied_arr, Len)
 
# This code is contributed by
# Mohit kumar 29


C#
// C# program to copy the contents
// of one array into another
// in the reverse order
using System;
class GFG
{
 
// Function to print the array
static void printArray(int []arr, int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        Console.Write(arr[i]);
    }
}
 
// Driver code
public static void Main()
{
    int []original_arr = {1, 2, 3, 4, 5};
    int len = original_arr.Length;
    int []copied_arr = new int[len];
    int i;
     
    // Copy the elements of the array
    // in the copied_arr in Reverse Order
    for (i = 0; i < len; i++)
    {
        copied_arr[i] = original_arr[len - i - 1];
    }
 
    // Print the original_arr
    Console.Write("\nOriginal array: ");
    printArray(original_arr, len);
 
    // Print the copied array
    Console.Write("\nResultant array: ");
    printArray(copied_arr, len);
    }
}
 
// This code is contributed by Rajput-Ji


PHP


输出:
Original array: 1 2 3 4 5 
Resultant array: 5 4 3 2 1

时间复杂度: O(len)

辅助空间: O(len)

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。