编译器对 Project Euler #22 给出了不同的答案

发布于 2024-11-19 21:36:10 字数 1776 浏览 2 评论 0 原文

我正在做欧拉项目#22:

使用名称.txt(右键单击并“将链接/目标另存为...”),一个 46K 文本 包含超过五千个名字的文件,首先对其进行排序 按字母顺序排列。然后计算出字母值 每个名称,将该值乘以它在 列表以获得名称分数。

例如,当列表按字母顺序排序时,COLIN, 价值 3 + 15 + 12 + 9 + 14 = 53,是第 938 个名字 列表。因此,COLIN 的得分为 938 × 53 = 49714。

文件中所有姓名分数的总和是多少?

使用 Cygwin 的 gcc-g++ 编译器编译下面的代码,答案是 871129635。但对于 Visual Studio 2008,答案是正确的,871198282。为什么会这样呢?

#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;

bool strCmp(string x, string y) {
    if(x.compare(y) == -1)
        return true;
    else
        return false;
}

int getScore(string s) {
    int score = 0;
    for(unsigned int i = 0; i < s.length(); i++)
        score += (((int) s.at(i)) - 64);
    return score;
}

int getTotalScore(vector<string> names) {
    int total = 0;
    for(unsigned int i = 0; i < names.size(); i++)
        total += (getScore(names[i]) * (i+1));
    return total;
}

int main() {
    vector<string> names;
    ifstream namesFile("names.txt");

    char curChar;
    string curName = "";

    //get names from file
    if(namesFile.is_open()) {
        while(!namesFile.eof()) {
            curChar = namesFile.get();

            if(isalpha(curChar))
                curName.push_back(curChar);
            else {
                if(!curName.empty()) {//store finished name
                    names.push_back(curName);
                    curName.clear();
                }
            }
        }
    }
    namesFile.close();

    //alphabetize
    sort(names.begin(), names.end(), strCmp);

    //count up name scores
    cout << getTotalScore(names) << endl;
    return 0;
}

I'm doing Project Euler #22:

Using names.txt (right click and 'Save Link/Target As...'), a 46K text
file containing over five-thousand first names, begin by sorting it
into alphabetical order. Then working out the alphabetical value for
each name, multiply this value by its alphabetical position in the
list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN,
which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the
list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

Compiling my code below with Cygwin's gcc-g++ compiler, the answer is 871129635. But with Visual Studio 2008, the answer is correct, 871198282. Why is this the case?

#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;

bool strCmp(string x, string y) {
    if(x.compare(y) == -1)
        return true;
    else
        return false;
}

int getScore(string s) {
    int score = 0;
    for(unsigned int i = 0; i < s.length(); i++)
        score += (((int) s.at(i)) - 64);
    return score;
}

int getTotalScore(vector<string> names) {
    int total = 0;
    for(unsigned int i = 0; i < names.size(); i++)
        total += (getScore(names[i]) * (i+1));
    return total;
}

int main() {
    vector<string> names;
    ifstream namesFile("names.txt");

    char curChar;
    string curName = "";

    //get names from file
    if(namesFile.is_open()) {
        while(!namesFile.eof()) {
            curChar = namesFile.get();

            if(isalpha(curChar))
                curName.push_back(curChar);
            else {
                if(!curName.empty()) {//store finished name
                    names.push_back(curName);
                    curName.clear();
                }
            }
        }
    }
    namesFile.close();

    //alphabetize
    sort(names.begin(), names.end(), strCmp);

    //count up name scores
    cout << getTotalScore(names) << endl;
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˉ厌 2024-11-26 21:36:10

这里:

if(x.compare(y) == -1)

您假设 std::string::compare 将为小于结果返回 -1,但实际上它可以返回任何负值。您可以使用 x.compare(y) x.compare(y) x.compare(y) x.compare(y) x.compare(y) < 来修复此问题。 0,但最好只写x。事实上,您甚至不需要 strCmp 函数,因为 std::sort 的默认行为是使用 operator< 比较元素。

Here:

if(x.compare(y) == -1)

You are assuming that std::string::compare will return -1 for a less-than result, but it can in fact return any negative value. You can fix this by using x.compare(y) < 0, but it's better to just write x<y. In fact, you don't even need the strCmp function because the default behavior of std::sort is to compare the elements using operator<.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文