2012-11-19 56 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項