Вопрос задан 24.07.2018 в 11:30. Предмет Информатика. Спрашивает Миронов Александр.

Дан одномерный массив размером N . Перевернуть массив о обратном порядке, именно перевернуть, а не

вывести в обратном порядке!! Входные данные Вводится число N (0 Выходные данные Выведите массив N чисел согласно условию задачи. Пример Вход Выход 7 2 4 1 3 5 3 1 1 3 5 3 1 4 2 Решать именно через массив
0 0
Перейти к ответам

Ответы на вопрос

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Саксонов Алексей.

C++
#include <iostream>
#include <algorithm>
#include <iterator>
#include <random>
#include <vector>
 std::mt19937 gen {std::random_device()()};
std::uniform_int_distribution<int> uid(1, 10);  
int main()
{
     int N = 0;
     std::cin >> N;
     std::vector<unsigned> arr(N);
     for (auto &i : arr) i = uid(gen);
     std::reverse(std::begin(arr), std::end(arr));
     std::copy(std::begin(arr), std::end(arr),
                                             std::ostream_iterator<int>(std::cout, " "));
     system("pause");
}

0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

To reverse an array in-place, you can use a two-pointer approach. Here's how you can do it in Python:

```python def reverse_array(arr): left = 0 right = len(arr) - 1

while left < right: arr[left], arr[right] = arr[right], arr[left] left += 1 right -= 1

# Example usage N = int(input("Enter the size of the array: ")) array = [] for i in range(N): num = int(input("Enter a number: ")) array.append(num)

reverse_array(array) print("Reversed array:", array) ```

This code defines a function `reverse_array` that takes an array as input and reverses it in-place using two pointers. The `left` pointer starts from the beginning of the array, and the `right` pointer starts from the end of the array. In each iteration, the values at the `left` and `right` indices are swapped, and the pointers are moved towards the center of the array. The process continues until the `left` pointer surpasses the `right` pointer.

To test the code, you can run it and provide the size of the array (`N`) and the elements of the array. The code will then reverse the array and print the reversed array.

Please note that the code assumes the input is provided through the console. If you are using a different input method, you may need to modify the code accordingly.

0 0

Топ вопросов за вчера в категории Информатика

Последние заданные вопросы в категории Информатика

Задать вопрос