2014-10-02 138 views
1

最小值我有兩個數組:查找陣列

int playerSums[9] = { }; 
string playerNames[9] = { }; 

我試圖讓陣列playerSums最小值,也該值的數組索引

這裏是我試過到目前爲止:

if (playerNames[index] == "End" || playerNames[index] == "end") { 
    int lowestValue = playerSums[0]; 
    for (i = 1; i < sizeof(playerSums)/sizeof(playerSums[0]); i++) { 
     if (playerSums[i] < lowestValue || lowestValue != 0) 
      lowestValue = playerSums[i]; 
    } 
    cout << index[playerNames] << " had the lowest values and got the sum "; 
    cout << lowestValue << endl; 
} 

如何找到和陣列playerSums中顯示的最小值例如如果只有3名球員被打,即僅3元數組被填充(並且其餘的元素等於零)?

我需要索引來顯示獲得最小值的玩家的名字。

+2

請解釋您當前的代碼有錯誤。當你在這裏,解釋內部'if'語句背後的邏輯:) – 2014-10-02 17:40:24

+0

你見過http://stackoverflow.com/questions/23871658/cs-min-element-not-working-for-array? – matsjoyce 2014-10-02 17:41:31

+4

'std :: vector'讓生活變得如此簡單。 – 2014-10-02 17:42:52

回答

3

您可以使用標準算法std::min_element在頭<algorithm>宣稱找到元素WITN最小總和。例如

#include <algorithm> 

int *min = std::min_element(playerSums, playerSums + 3); 

std::cout << playerNames[min - playerSums] 
      << " had the lowest values and got the sum " << *min 
      << std::endl; 

同樣可以使用標準功能std::beginstd::endstd::distance使用的算法,您可以編寫類似的算法自己功能的頭部聲明<iterator>

#include <algorithm> 
#include <iterator> 

int *min = std::min_element(std::begin(playerSums), std::end(playerSums)); 

std::cout << playerNames[ std::distance(playerSums, min)] 
      << " had the lowest values and got the sum " << *min 
      << std::endl; 

而是被寫入。例如

size_t min_sum(int playerSums[], size_t n) 
{ 
    size_t min = 0; 

    for (size_t i = 1; i < n; i++) 
    { 
     if (playerSums[min] < playerSums[i]) min = i; 
    } 

    return min; 
} 

size_t min = min_sum(playerSums, sizeof(playerSums)/sizeof(*playerSums) ); 

std::cout << playerNames[min] 
      << " had the lowest values and got the sum " << playerSums[min] 
      << std::endl; 

如果您需要跳過等於零,則數組元素的功能將類似於

size_t min_sum(int playerSums[], size_t n) 
{ 
    size_t min = 0; 

    while (min < n && playerSums[i] == 0) ++min; 

    for (size_t i = min; i < n; i++) 
    { 
     if (playerSums[min] < playerSums[i]) min = i; 
    } 

    return min; 
} 

size_t min = min_sum(playerSums, sizeof(playerSums)/sizeof(*playerSums) ); 

if (min != sizeof(playerSums)/sizeof(*playerSums)) 
{ 
    std::cout << playerNames[min] 
       << " had the lowest values and got the sum " << playerSums[min] 
       << std::endl; 
} 
2

你知道你分配給lowestValue當您更改該變量的值的元素的索引,所以只保存指數的變量(比如,index),這樣,當你完成index擁有的最後一個值的索引分配。

2

首先調整你的循環條件。我不確定你以前是否定義過我,所以你可能忘記了。第二個停止條件是sizeof(palyerSums)就足夠了。你也只需要存儲數組中最低的playerSums的索引。 if條件也有太多的東西。如果最低值不爲零,您將始終更改該值,除非最低值恰好爲零,否則這看起來不正確。

int lowestValue = playerSums[0]; 
int resultIndex = 0; 
for(int i = 1; i < sizeof(playerSums)/sizeof(playerSums[0]); i++) { 
    if(playerSums[i] < lowestValue) { 
    lowestValue = playerSums[i]; 
    resultIndex = i; 
    } 
} 
cout << playerNames[resultIndex] << "blabla" << lowestValue; // instead of lowestValue you could also do playerSums[resultIndex] ofcourse. 

讓我知道是否可行

+0

數組的大小!=數組的長度 – P0W 2014-10-02 18:04:54

+0

我的不好我不熟悉C++,但是這對於那個部門是有意義的。 – Juru 2014-10-03 12:47:39

3

您存儲lowestValue值最低的同樣的方式,存儲索引在一個變量中,比方說,lowestValueIndex。此外,刪除外,如果和移動它內部的for循環:

if(playerNames[i] == "End" || playerNames[i] == "end") 
    break; 

這樣,您將確保只有誰在玩的玩家將被處理。此外,您不需要檢查最低值是否爲零。因此,代碼會看起來像:

int lowestValue = playerSums[0]; 
int lowestValueIndex = 0; 
for (int i = 1; i < sizeof(playerSums)/sizeof(playerSums[0]); ++i) 
{ 
    if(playerNames[i] == "End" || playerNames[i] == "end") 
     break; 
    if (playerSums[i] < lowestValue) 
    { 
      lowestValue = playerSums[i]; 
      lowestValueIndex = i; 
    } 
} 
cout << index[playerNames] << " had the lowest values and got the sum " 
    << lowestValue << endl; 

正如一個音符,使用可以長到簡化這個(如vector)的標準數組:

std::vector<std::string> playerNames; 
std::vector<int> playerSums; 

for (int i = 1; i < playerSums.size(); ++i) 
{ 
    if (playerSums[i] < lowestValue) 
    { 
      lowestValue = playerSums[i]; 
      lowestValueIndex = i; 
    } 
} 
cout << index[playerNames] << " had the lowest values and got the sum " 
    << lowestValue << endl; 
3

像往常一樣,最簡單的解決方法是使用標準庫,例如

int lowestValue = *it; 

如果只想遍歷數組中的第3個元素,那麼你可以做這樣的事情:

auto it = std::min_element(std::begin(playerSums), std::end(playerSums)); 
std::size_t index = std::distance(std::begin(playerSums), it); 

現在,您可以通過提領該迭代it得到最小值而不是:

auto first = std::begin(playerSums); 
auto it = std::min_element(first, std::next(first, 3)); 
std::size_t index = std::distance(first, it); 

注:喜歡std::next而不是簡單的指針算術(例如playerSums + 3),因爲它更通用(適用於所有迭代器類型)。

+0

「取消引用'it'」表示寫入* *。 – GingerPlusPlus 2014-10-02 17:54:20

+0

@GingerPlusPlus是的,使用* dereference操作符*'*'。 – Snps 2014-10-02 17:55:32

+2

我讀到這個問題的方式是,陣列分配給多達9名玩家,但玩家數量可能會少於玩遊戲。假設未使用的數組條目填充了零,那麼如何找到忽略未使用條目的最小值? – 2014-10-02 17:56:20