Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何列出得分最高的单词_C++ - Fatal编程技术网

C++ 如何列出得分最高的单词

C++ 如何列出得分最高的单词,c++,C++,我试图找出如何列出得分最高的单词,基于这个函数来计算分数,单词来自一个单词数组,等等。如何解决这个问题?因为你只需要得分最高的单词,所以不需要跟踪所有候选单词的得分。跟踪最好的一个就足够了 string best_word; int best_score = 0; for (auto word &: all_the_words) { int cur_score = ScrabbleScore(word); if (cur_score > best_score) {

我试图找出如何列出得分最高的单词,基于这个函数来计算分数,单词来自一个单词数组,等等。如何解决这个问题?

因为你只需要得分最高的单词,所以不需要跟踪所有候选单词的得分。跟踪最好的一个就足够了

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.
vector最佳单词;
int最佳分数=0;
for(自动字和:所有字){
int cur_分数=拼字分数(单词);
如果(当前分数>最佳分数){
最好的词。清楚();
最好的单词。推回(单词);
最佳分数=当前分数;
}否则如果(当前分数==最佳分数){
最好的单词。推回(单词);
}
}
//现在你有了最好的单词和最好的分数。

因为您只需要得分最高的单词,所以不需要跟踪所有候选单词的得分。跟踪最好的一个就足够了

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.
vector最佳单词;
int最佳分数=0;
for(自动字和:所有字){
int cur_分数=拼字分数(单词);
如果(当前分数>最佳分数){
最好的词。清楚();
最好的单词。推回(单词);
最佳分数=当前分数;
}否则如果(当前分数==最佳分数){
最好的单词。推回(单词);
}
}
//现在你有了最好的单词和最好的分数。

您可以将您的字串放入
std::vector
,并在该向量上调用
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;
    }
}
#include//for std::sort
#include//for std::exception
#include//for std::cout
#include//for std::runtime\u错误
#include//for std::string
#include//for std::vector
使用名称空间std;
//注#1:由于此函数是*观察*单词参数,
//通过常量引用传递它(常量字符串和单词)。
int拼字分数(常量字符串和单词){
智力得分=0;
静态常量字符记分表[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(自动字母:word){
//如果字母词
如果(字母>='a'&&字母拼字分数(rhs);
}
);
//打印结果

cout您可以将您的字串放入
std::vector
,并在该向量上调用
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;
    }
}
#include//for std::sort
#include//for std::exception
#include//for std::cout
#include//for std::runtime\u错误
#include//for std::string
#include//for std::vector
使用名称空间std;
//注#1:由于此函数是*观察*单词参数,
//通过常量引用传递它(常量字符串和单词)。
int拼字分数(常量字符串和单词){
智力得分=0;
静态常量字符记分表[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(自动字母:word){
//如果字母词
如果(字母>='a'&&字母拼字分数(rhs);
}
);
//打印结果

不能处理什么?还有为什么
else{Letter='-';…}
?Scrabble:嗯,那些讨厌的空白瓷砖呢?建议将
else{
更改为
else如果(Letter!='''.{/code>。处理什么?还有为什么
else{Letter='-';…}
?Scrabble:嗯,那些讨厌的空白瓷砖呢?建议更改
else{
into
else if(字母!=''){
+1 for
**static const**char scoreTable[26]
。嘿,感谢这段代码,我正在试图理解它是如何工作的。我唯一不理解的代码部分是
for(const auto&w:words)
我在哪里可以读到这个?对列表进行排序以找到最大值的效率非常低。@Hayde:for
的上述
是一个C++11“for each”。它的意思是:
for(const string&w:words)
(即“for words vector中的每个字符串”)。我使用了
const&
而不仅仅是
string
,因为for循环正在观察项目,所以我使用了
const
引用;您可以找到更多细节。@Dukeling:我同意;如果只需要最大值,有更好的方法。我认为更多的是一般向量“自定义排序”解决方案,同时列出其他(“非最大分数”)单词的分数。
**静态常量**字符分数表[26]
为+1。嘿,感谢这段代码,我正在试图理解它是如何工作的。我不理解的代码中唯一的部分是
for(const auto&w:words)
我在哪里可以读到这个?对列表进行排序以找到最大值的效率非常低。@Hayde:for
的上述
是一个C++11“for each”。它的意思是:
for(const string&w:words)
(即“for words vector中的每个字符串”)。我使用了
const&
而不仅仅是
string
,因为for循环正在观察项目,所以我使用了
const
引用;您可以找到更多细节。@Dukeling:我同意;如果只需要最大值,有更好的方法。我认为更多的是一般向量“自定义排序”解决方案,同时列出其他(“非最高分数”)单词的分数。