如何交换数组中的最小和最大位置? [英] How to swap the positions of Min and Max in an array?

查看:250
本文介绍了如何交换数组中的最小和最大位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在代码中,我找到了任何给定数组的 min max 值。现在,我想交换他们的职位,并打印出来。就像 Min 位于 Max 的位置一样,反之亦然。我该如何改变他们的位置?我猜我做错了。

Here in the code I found the min and max values of any given array . Now I want to swap their positions, and print them out. Like Min at position of Max and vice versa. How can I change their positions? I have done it wrong, I guess.

#include <iostream>

using namespace std;

int main()
{
    int array[8] = { 0, 0, 0, 0, 0, 0, 0, 0}; 
    int min = array[0]; 
    int max = array[0]; 
    int indexOfMin = 0; 
    int indexOfMax = 0; 
    int arrSize = sizeof(array)/sizeof(array[0]); 
    int temp = 0; 

    cout << "Enter an array: "; 

    int k;
    for(k = 0; k <= arrSize; k++){ 
        cin >> array[k];
    }

    for (int i = 0; i < arrSize; i++){ 
         if(array[i] >= max ){          
            max = array[i];            
            indexOfMax = i;            
        }
    }

    for (int i = 0; i < arrSize; i++){ 
        if(array[i] == min){           
            continue;
        }
        if(array[i] < min){
            min = array[i];
            indexOfMin = i;
        }
    }

    temp = min;
    min = max;
    max = temp;

    cout << array[k] << " " <<endl;

    return 0;
}

输入= 1、5、9、1 2,9,1,3

输出= 9,5,9,1,2,1,1,3

推荐答案


int min = array[0];
int max = array[0];


您还不知道。程序此时的 array [0] 0 ... ...但 0 在用户输入后可能不是数组的元素。

You don't know that yet. array[0] at this point of the program is 0 ... but 0 might not be an element of the array after user input.


int indexOfMin = 0;
int indexOfMax = 0;


内存中对象的索引和大小应为 std :: size_t < cstddef> ),因为可以保证 std :: size_t 足够大。 int 没有这样的保证。

Indexes into and the sizes of objects in memory should be of type std::size_t (<cstddef>) because it is guaranteed that std::size_t is big enough. There is no such guarantee for int.


int arrSize = sizeof(array) / sizeof(array[0]);


使用 std :: size() < iterator> )以获得更清晰的代码:

Use std::size() (<iterator>) for clearer code:

auto const arrSize{ std::size(array) };




int k;
for (k = 0; k <= arrSize; k++) {
    cin >> array[k]; 
}


有效的数组索引范围为 0 到< N 表示数组 array [N] 。您无法访问数组。使用 k< arrSize 作为条件。 k 的类型应为 std :: size_t

Valid array indexes range from 0 to < N for an array array[N]. You access the array out of bounds. Use k < arrSize as condition. k should be of type std::size_t.


for (int i = 0; i < arrSize; i++) {
  if (array[i] >= max) {
      max = array[i];
      indexOfMax = i;
  }
}

for (int i = 0; i < arrSize; i++) {
  if (array[i] == min) {
      continue;
  }
  if (array[i] < min) {
      min = array[i];
      indexOfMin = i;
  }
}


如果在用户输入后定义 int min = array [0]; int max = array [0]; 这些循环的 i = 1 if(array [i] == min){继续; } 什么也买不到。相反,它浪费了额外的比较时间。同样,这两个循环可以合并为一个循环:

If you had defined int min = array[0]; and int max = array[0]; after user input you could start these loops with i = 1. The if (array[i] == min) { continue; } buys you nothing. In contrary it wastes time with an additional comparison. Also, both loops can be combined into one:

int min{ array[0] };
int max{ array[0] };

std::size_t indexOfMin{ 0 };
std::size_t indexOfMax{ 0 };

for (size_t i{ 1 }; i < arrSize; ++i) {
    if(array[i] < min) {
        min = array[i];
        indexOfMin = i;
    }
    else if(array[i] > max) {
        max = array[i];
        indexOfMax = i;
    }
}




temp = min;
min = max;
max = temp;


将交换变量的值最小值最大值。另外,如果可以用这种方法交换数组中的最小值和最大值,为什么还要记住它们的位置?尝试

Will swap the values of the variables min and max. Also, if swapping the minimal and maximal values in the array could be done that way, why remember their position? Try

temp = array[indexOfMin];
array[indexOfMax] = array[indexOfMin];
array[indexOfMin = temp];




所以最后我只写了

So at the end i just write

for (k = 0; k <= 7; k++) {
    cout << array[k] << " " << endl;
}

不,你写

for (std::size_t k = 0; k < arrSize; k++) {
    std::cout << array[k] << " ";
}
std::cout.put('\n');

因为您(应该)已经声明了先前的 k 从for循环内的输入循环开始,您就习惯了在尽可能接近变量的地方声明和定义变量。另外,由于要在一行中显示列表,因此不要在循环内使用 std :: endl ,而是打印'\n'

because you (should) have declared the previous k from the input loop inside the for-loop and you make it a good habit to declare and define variables as close to where they're used as possible. Also, since you want a list in one line don't use std::endl inside the loop but print a '\n' afterwards.

这篇关于如何交换数组中的最小和最大位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆