Input : arr[ ] = { 10, 20, 30, 40, 50, 60 } Output : { 60, 10, 50, 20, 40, 30 } Explanation : array is rearranged in the form { 1st max, 1st min, 2nd max, 2nd min, 3rd max, 3rd min } Input : arr [ ] = { 15, 17, 19, 23, 36, 67, 69 } Output : { 69, 15, 67, 17, 36, 19, 23 }
#include <bits/stdc++.h> using namespace std; int main () { int arr[] = { 1, 2, 3, 4, 5, 6 }; int n = sizeof (arr) / sizeof (arr[0]); // creating a new array to store the rearranged array. int final[n]; // pointing variables to initial and final element index. int min = 0, max = n - 1; int count = 0; // iterating over the array until max is less than or equals to max. for (int i = 0; min <= max; i++) { // if count is even then store max index element if (count % 2 == 0) { final[i] = arr[max]; max--; } // store min index element else { final[i] = arr[min]; min++; } count++; } // printing the final rearranged array. for (int i = 0; i < n; i++) cout << final[ i ] << " "; return 0; }
6 1 5 2 4 3
以上がC++を使用して配列を最大-最小形式に再配置しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。