简体   繁体   English

如何交换数组中的最后一个元素和最大元素?

[英]How do I swap the last and maximum element in the array?

How to swap the maximum and last element of an array?如何交换数组的最大和最后一个元素?

I want to write the program which requests natural number NN (no more than 100), further sequence from NN of various numbers, changes places of the last element and the maximum, leaving other elements without change, and deduces the received array.我想编写一个程序,它请求自然数NN(不超过100),从各种数的NN进一步序列,改变最后一个元素和最大值的位置,保持其他元素不变,并推导接收到的数组。

Sample Input 1:样本输入 1:

8
3 6 12 1 7 19 25 4

Sample Output 1:样品 Output 1:

3 6 12 1 7 19 4 25

This is what I tried:这是我尝试过的:

#include <iostream>
using namespace std;
int main() 
{
    int i,arr[100],n;
    cin>>n;
    int imin=0;
    for (int i=0; i<n; i++){cin>>arr[i];}

    int max=0;
    for (i=1;i<n;i++) 
    {
        if (arr[i]>arr[max])
            max=i;
        int tmp=arr[max];
        arr[max]=arr[n-1];
        arr[n-1]=tmp;
        for (i=0;i<n;i++)
            cout <<arr[i]<< " ";
        cout << "\n";  
    }  
    return 0;
}

It does not change places the maximum and the last.它不会改变最大和最后的位置。 How can I do this?我怎样才能做到这一点?

I recommend using a loop to find the index of the maximum value, then outside of the loop swap the values .我建议使用循环来查找最大值的索引,然后在循环之外交换值

int index = 0;
int max_value = arr[0];
int index_of_max_value = 0;
for (index = 1; index < n; ++n)
{
    if (arr[index] > max_value)
    {
        max_value = arr[index];
        index_of_max_value = index;
    }
}
std::swap(arr[index_of_max_value], arr[n-1]);
#include <algorithm> // max_element, iter_swap

// ...

    auto me = std::max_element(arr, arr + n); // get an iterator to the largest element
    std::iter_swap(me, arr + n - 1); // swap the value at "me" with the last element

To swap the last element of the array with the maximum element of the array, first you need to find the index of the maximum element by traversing the full array.要将数组的最后一个元素与数组的最大元素交换,首先需要通过遍历整个数组找到最大元素的索引。 Then swap the last index of the array with the index that you get for maximum element.然后将数组的最后一个索引与您为最大元素获得的索引交换。 Your code had some mistakes i fixed them, have a look.你的代码有一些错误,我修复了它们,看看。 Also see the indentation that i did, it is a good practice and it makes the code more redable.另请参阅我所做的缩进,这是一个很好的做法,它使代码更加可修改。

swap(x, y) is a builtin function to swap two variable values. swap(x, y)是一个内置的 function 交换两个变量值。

You can do this like:你可以这样做:

#include <iostream>
using namespace std;
int main() 
{
    int n;

    cin>>n;
    
    //int arr[n]; 
    // as said in the comment it's not the standard way to declare variable length array in C++, either use fixed length or use vector in c++

    vector<int> arr(n + 1);

    for (int i = 0; i < n; i++) cin >> arr[i];

    int mx = 0, idx;

    for(int i = 0; i < n; i++){
        if(arr[i] > mx){
            mx = arr[i];
            idx = i;
        }
    }

    swap(arr[n-1], arr[idx]); // swap is a builtin function
   
    for(int i = 0; i < n; i++)
         cout << arr[i] << " ";
    cout << "\n";  
 }  
return 0;}

First, you have to find the largest element index.首先,您必须找到最大的元素索引。 Then swap it with the last element.然后将其与最后一个元素交换。 Some cases may occur that the last element is the largest element so the array doesn't change at all or there are multiple maximum elements so you have to decide what to do next which element you want to swap, the first maximum element or the other one with the last element of the array.在某些情况下,可能会出现最后一个元素是最大元素,因此数组根本不会改变,或者有多个最大元素,因此您必须决定下一步要交换哪个元素,第一个最大元素或另一个一个与数组的最后一个元素。

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int n;
    cin >> n;
    vector<int> arr(n + 1);
    for(int i = 0; i < n; i++){
        cin >> arr[i];
    }

    int maxi = 0;
    for(int i = 0; i < n; i++){
        if(arr[i] > arr[maxi])
            maxi = i;
    }
    // swap is a builtin function that interchange two elements
    swap(arr[maxi], arr[n - 1]); 
    for(int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << "\n";
    return 0;
}

Sample Input:样本输入:

8
3 6 12 1 7 19 25 4
4
1 2 3 4

Sample Output:样品 Output:

3 6 12 1 7 19 4 25
1 2 3 4
#include <iostream>
using namespace std;
int main() 
    {
    int i,arr[100],n;
    cin>>n;
    int imin=0;
    for (int i=0; i<n; i++){cin>>arr[i];}

    int max=0;
    for (i=1;i<n;i++) 
    {
        if (arr[i]>arr[max])
            max=i;
    } // closed the for loop here//

    int tmp=arr[max];
    arr[max]=arr[n-1];
    arr[n-1]=tmp;
    for (i=0;i<n;i++)
         cout <<arr[i]<< " ";
     cout << "\n";  
 
     return 0;
    }

Your code is good just needed to close the for loop after the max=i statement.您的代码很好,只需要在 max=i 语句之后关闭 for 循环。 If you have any other issue then ask.如果您有任何其他问题,请询问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM