如何降低最低值

How do I drop the lowest value?

本文关键字:最低值 何降低      更新时间:2023-10-16

我对C++还很陌生,我需要帮助找出删除随机生成的一组数字的最低值的代码。这是我到目前为止的代码:

//Create array and populate the array with scores between 55 and 10
//  Drop lowest Score 
#include <iostream>
#include <cstdlib>//for generating a random number
#include <ctime>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;

//function prototype
int *random (int);

int main()
{   int *numbers; //point to numbers
//get an array of 20 values
numbers = random(20);
//display numbers
for (int count = 0; count < 20; count++)
cout << numbers[count] << endl;
cout << endl;

system("pause");
return 0;
}
//random function, generates random numbers between 55 and 100 ??
int *random(int num)
{   int *arr; //array to hold numbers
//return null if zero or negative
if (num <= 0)
return NULL;
//allocate array
arr = new int[num];
//seed random number generator
srand(time (0));
//populate array
for (int count = 0; count < num; count++)
arr[count] = (rand()%(45) +55);
//return pointer
//
return arr;
}

对于这段代码,在函数返回随机数后,我将如何排序或找到最低分数来删除它?

int main()
{   int *numbers; //point to numbers
//get an array of 20 values
numbers = random(20);
//display numbers
for (int count = 0; count < 20; count++)
cout << numbers[count] << endl;
cout << endl;

system("pause");
return 0;
}

非常感谢您的建议!

通常,要找到数组中的最低值,可以使用以下psuedo算法:

min = array[0] // first element in array
for (all_values_in_array)
{
if (current_element < min)
min = current_element
}

但是,您不能从静态数组中"删除"一个值。您可以考虑使用动态容器(例如向量),或者将最低值与最后一个值交换,并假装数组的大小小于1。另一个低级选项是在堆上创建自己的动态数组,但是,这可能比您想要的更复杂。

使用矢量会容易得多。要删除最低的元素,只需按相反的顺序排序,然后删除最后一个元素。就我个人而言,我建议使用矢量。

查找最小元素的明显方法是使用std::min_element()。您可能想要使用std::vector<T>来保存元素,但这并不是绝对必要的。你可以从这样的数组中删除最小的值:

if (count) {
int* it = std::min_element(array, array + count);
std::copy(it + 1, array + count--, it);
}

假设您合理地使用std::vector<int>,代码将如下所示:

if (!array.empty()) {
array.erase(std::min_element(array.begin(), array.end()));
}

首先找到最低数字的索引:

int lowest_index=0, i;
for (i=0; i<20; i++)
if (arr[i]<arr[lowest_index])
lowest_index=i;

现在我们知道了索引,移动该索引后面的数字以覆盖我们找到的索引。要移动的数字数量将是19减去找到的索引。即,如果索引2(第三个数字,因为第一个在索引0)是最低的,那么在该索引之后有17个数字,所以这就是我们需要移动的数字。

memcpy(&arr[lowest_index],&arr[lowest_index+1],sizeof(int)*(19-lowest_index))

祝你好运!

对数组升序排序
最小值将位于数组的开头。

或者按降序排列数组并删除最后一个元素。

除了其他人所说的,您还可以选择使用类似的东西,也许是std::list。它内置了排序功能,还可以为两个元素定义自己的比较函数。(虽然对于int,这不是必要的)

首先,我通常用它将包含的元素的类型来定义向量或列表。接下来,对于列表,我为其定义了一个迭代器——尽管这两者都只是一种方便,但都不是必需的。

一旦你有了一个包含int的列表,就把它们添加到其中。习惯和不需要做其他事情意味着我会使用.push_back来添加每个新元素。完成后,我将对列表进行排序,获取值最低的元素(也是最低的"index"-第一个项),然后最后删除该项。

一些代码值得思考:

#include <cstdio>
#include <cstdlib>
#include <list>

using namespace std;
typedef list<int> listInt;
typedef listInt::iterator listIntIter;
bool sortAsc(int first, int second)
{
return first < second;
}
bool sortDesc(int first, int second)
{
return first > second;
}
int main (void)
{
listInt mList;
listIntIter mIter;
int i, curVal, lowestScore;
for (i=1; i<=20; i++)
{
curVal = rand()%45 + 55;
mList.push_back(curVal);
printf("%2d. %dn", i, curVal);
}
printf("n");
mList.sort();
//    mList.sort(sortAsc);  // in this example, this has the same effect as the above line.
//    mList.sort(sortDesc);
i = 0;
for (mIter=mList.begin(); mIter!=mList.end(); mIter++)
printf("%2d. %dn", ++i, *mIter);
printf("n");
lowestScore = mList.front();
mList.pop_front();
printf("Lowest score: %dn", lowestScore);
return 0;
}

哦,选择使用printf而不是cout也是经过深思熟虑的。有几个原因。

  1. 个人偏好-我发现键入printf("%dn", someVar);更容易比cout << someVar << endl;
  2. 大小-在windows下使用gcc构建,本例的发布模式exe为21kb。使用cout,它可以跳到459kb——同样的功能!尺寸增加20倍却没有收益?不,谢谢

这里有一个std::列表参考:http://www.cplusplus.com/reference/stl/list/

在我看来,你的问题的最佳解决方案是使用链表来存储数字,这样你就可以使用复杂度O(N)=N的算法来查找列表中的最小元素,这是user1599559或Mikael Lindqvist给出的类似查找方法,您只需要将指向存储该项目的链表中的项目(ItemX)的指针与最小值一起存储,然后要消除Item Xtem X-1指向ItemX+1和Item X 分配的可用内存