2013-09-24 3 views
1

나는 점수를 계산하는이 함수를 기반으로 가장 높은 점수를 가진 단어를 나열하는 방법을 찾고자합니다. 단어는 단어의 배열에서 나온 것입니다. 이 문제를 어떻게 해결할 수 있습니까? 그냥 가장 높은 점수 단어를 필요로하기 때문에가장 높은 점수를 가진 단어를 나열하는 방법

+2

_tackle의 what_

은 자세한 내용은 다음 샘플 코드를 참조하십시오? 또한 왜'else {Letter = '-'; ...}'? – P0W

+0

스크래블 : 음, 그 성가신 빈 타일은 어떨까요? 'else {'를'else if (Letter! = '') {'로 바꿀 것을 제안하십시오. – chux

답변

4

모든 후보의 단어를 점수를 추적 할 필요가 없습니다. 을 가장 잘 추적하면 하나면 충분합니다.

string best_word; 
int best_score = 0; 
for (auto word &: all_the_words) { 
    int cur_score = ScrabbleScore(word); 
    if (cur_score > best_score) { 
     best_word = word; 
     best_score = cur_score; 
    } 
} 
// Now you have best_word and best_score. 

편집 : 같은 최고 점수로 모든 단어를 돌봐 확장합니다.

vector<string> best_words; 
int best_score = 0; 
for (auto word &: all_the_words) { 
    int cur_score = ScrabbleScore(word); 
    if (cur_score > best_score) { 
     best_words.clear(); 
     best_words.push_back(word); 
     best_score = cur_score; 
    } else if (cur_score == best_score) { 
     best_words.push_back(word); 
    } 
} 
// Now you have best_words and best_score. 
1

당신은 std::vector<std::string>에 단어 문자열을 놓고, 그들의 "점수"에 의해 단어를 정렬 할 사용자 정의 비교 함수을 지정하는 벡터에 std::sort() 알고리즘을 호출 할 수 있습니다.

#include <algorithm> // for std::sort 
#include <exception> // for std::exception 
#include <iostream>  // for std::cout 
#include <stdexcept> // for std::runtime_error 
#include <string>  // for std::string 
#include <vector>  // for std::vector 
using namespace std; 

// NOTE #1: Since this function is *observing* the "word" parameter, 
// pass it by const reference (const string & word). 
int ScrabbleScore(const string & word) { 
    int score = 0; 
    static const char scoreTable[26] = { 
     1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 
     5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 
     1, 4, 4, 8, 4, 10 
    }; 

    for (auto letter : word) { 
     // if alphabet word 
     if (letter >= 'a' && letter <= 'z') { 
      score += scoreTable[letter - 'a']; 
     } else { 
      // NOTE #2: Throw an exception when an invalid 
      // letter is found. 
      throw runtime_error("Invalid letter in word."); 
     } 
    } 
    return score; 
} 

int main() { 
    // Some test words 
    vector<string> words = { 
     "hi", "hello", "world", "ciao", 
     "integer", "sum", "sort", "words" 
    }; 

    // Sort vector by ScrabbleScore (descending order) 
    sort(words.begin(), words.end(), 
     [](const string& lhs, const string& rhs) { 
      return ScrabbleScore(lhs) > ScrabbleScore(rhs); 
     } 
    ); 

    // Print result 
    cout << "<word> (<score>)" << endl; 
    cout << "------------------" << endl; 
    for (const auto & w : words) { 
     cout << w << " (" << ScrabbleScore(w) << ")" << endl; 
    } 
} 

출력 :

<word> (<score>) 
------------------ 
world (9) 
words (9) 
hello (8) 
integer (8) 
ciao (6) 
hi (5) 
sum (5) 
sort (4) 
+0

+1 ** 정적 const ** 문자 scoreTable [26]에 +1. – chux

+0

안녕하세요,이 코드를 주셔서 감사합니다, 내가 이해할 수없는 코드의 유일한 부분은'for (const auto & w : words)'입니다.이 부분을 어디에서 읽을 수 있습니까? – Hayde

+0

최대 값을 찾기 위해 목록을 정렬하는 것은 매우 비효율적입니다. – Dukeling

관련 문제