2012-11-19 55 views
1

我对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; 
    } 

您的建议非常感谢!

回答

3

一般情况下,找到一个数组中的最低值,你可以按照这个伪算法:

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

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

使用矢量会容易得多。要删除最低的元素,您只需要sort in reverse order,然后remove the last element。就个人而言,我会推荐使用矢量。

2

寻找最小元素的明显方法是使用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())); 
} 
0

首先找到最低的个数指标:

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)) 

祝你好运!

+0

那么分配的内存,这是C与其说C++ –

0

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

或者对数组进行降序排序并删除最后一个元素。

0

除了别人的说法,你也可以选择使用类似的东西,也许是std :: list。它内置了排序功能,还提供了为两个元素定义自己的比较功能的功能。 (虽然对于整数,这不是必需的)

首先,我通常使用它将包含的元素的类型typedef向量或列表。接下来,对于列表我键入了一个迭代器 - 虽然这两个只是一个方便,但都不是必需的。

一旦你有一个列表将保存整数,只需将它们添加到它。习惯,不需要做别的事情意味着我会使用.push_back来添加每个新元素。完成后,我将对列表进行排序,获取最低值的元素(也是最低的'索引' - 第一项),然后我将删除该项目。

一些代码来沉思在:

#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. %d\n", 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. %d\n", ++i, *mIter); 
    printf("\n"); 

    lowestScore = mList.front(); 
    mList.pop_front(); 
    printf("Lowest score: %d\n", lowestScore); 

    return 0; 
} 

哦,而选择用printf而不是COUT是故意的了。出于几个原因。

  1. 个人喜好 - 我觉得它更容易输入printf("%d\n", someVar);cout << someVar << endl;
  2. 大小 - 与Windows下的gcc构建,这个例子中的释放模式的exe是21KB。 使用cout,它跳跃到459kb - 为相同的功能! 20倍增长无增益?不用了,谢谢!!

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

0

在我看来,您的问题最优化的解决方案是使用链表存储号码,这样你可以使用一种算法复杂度为O(N)= N查找列表中最小的元素,它是user1599559或Mikael Lindqvist给出的类似查找方法,您只需要将最小值与指向项目的指针一起存储(ItemX )在存储它的链接列表中,然后消除项目X只是告诉项目X - 1项X + 1,并通过X项