簡體   English   中英

如何交換數組中的最后一個元素和最大元素?

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

如何交換數組的最大和最后一個元素?

我想編寫一個程序,它請求自然數NN(不超過100),從各種數的NN進一步序列,改變最后一個元素和最大值的位置,保持其他元素不變,並推導接收到的數組。

樣本輸入 1:

8
3 6 12 1 7 19 25 4

樣品 Output 1:

3 6 12 1 7 19 4 25

這是我嘗試過的:

#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;
}

它不會改變最大和最后的位置。 我怎樣才能做到這一點?

我建議使用循環來查找最大值的索引,然后在循環之外交換值

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

要將數組的最后一個元素與數組的最大元素交換,首先需要通過遍歷整個數組找到最大元素的索引。 然后將數組的最后一個索引與您為最大元素獲得的索引交換。 你的代碼有一些錯誤,我修復了它們,看看。 另請參閱我所做的縮進,這是一個很好的做法,它使代碼更加可修改。

swap(x, y)是一個內置的 function 交換兩個變量值。

你可以這樣做:

#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;}

首先,您必須找到最大的元素索引。 然后將其與最后一個元素交換。 在某些情況下,可能會出現最后一個元素是最大元素,因此數組根本不會改變,或者有多個最大元素,因此您必須決定下一步要交換哪個元素,第一個最大元素或另一個一個與數組的最后一個元素。

#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;
}

樣本輸入:

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

樣品 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;
    }

您的代碼很好,只需要在 max=i 語句之后關閉 for 循環。 如果您有任何其他問題,請詢問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM