通过平方求幂(Project Euler 99) 关于我的解决方案的提示

发布于 2024-11-29 23:31:41 字数 1559 浏览 0 评论 0原文

这就是我正在谈论的问题 http://projecteuler.net/index.php?section=problems&id=99

我的代码将正确编译并运行。我猜计算是混乱的地方。它告诉我第 633 行是最大的(欧拉项目说这是不正确的)。

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int poww(int base, int exp);
int main()
{
    //ignore messy/unused variables. I am desperate 
    int lineNumber = 0;
    string line; 
    int answerLine = 0;
    int max =0;
    int lineNum = 0;
    int answer =0;
    ifstream inFile;
    size_t location;
    string temp1,temp2;
    int tempMax = 0;
    int base,exp = 0;
    inFile.open("C:\\Users\\myYser\\Desktop\\base_exp.txt");
    while(getline(inFile,line))
    {
        lineNumber++;
        location = line.find(",");
        temp1 = line.substr(0,(int(location)));
        temp2 = line.substr((int(location)+1),line.length());
        //cout << temp1 << " " << temp2 << endl;
        base = atoi(temp1.c_str());
        exp =  atoi(temp2.c_str());
        tempMax= poww(base,exp);

        if (tempMax > max){
            max = tempMax;
            answer = base;
            answerLine = lineNumber;
        }

    }


    cout << answer << " " << answerLine;

    cin.get();
    return 0;
}
int poww(int base, int exp)
{
    int result = 1;
    while (exp)
    {
        if (exp & 1)
            result *= base;
        exp >>= 1;
        base *= base;
    }

    return result;
}

Here is the problem I am talking about
http://projecteuler.net/index.php?section=problems&id=99

My code will compile and run correctly. I am guessing the computation is where it is messing up. It is telling me that line number 633 is the largest (which project euler says is incorrect).

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int poww(int base, int exp);
int main()
{
    //ignore messy/unused variables. I am desperate 
    int lineNumber = 0;
    string line; 
    int answerLine = 0;
    int max =0;
    int lineNum = 0;
    int answer =0;
    ifstream inFile;
    size_t location;
    string temp1,temp2;
    int tempMax = 0;
    int base,exp = 0;
    inFile.open("C:\\Users\\myYser\\Desktop\\base_exp.txt");
    while(getline(inFile,line))
    {
        lineNumber++;
        location = line.find(",");
        temp1 = line.substr(0,(int(location)));
        temp2 = line.substr((int(location)+1),line.length());
        //cout << temp1 << " " << temp2 << endl;
        base = atoi(temp1.c_str());
        exp =  atoi(temp2.c_str());
        tempMax= poww(base,exp);

        if (tempMax > max){
            max = tempMax;
            answer = base;
            answerLine = lineNumber;
        }

    }


    cout << answer << " " << answerLine;

    cin.get();
    return 0;
}
int poww(int base, int exp)
{
    int result = 1;
    while (exp)
    {
        if (exp & 1)
            result *= base;
        exp >>= 1;
        base *= base;
    }

    return result;
}

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

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

发布评论

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

评论(2

季末如歌 2024-12-06 23:31:41

你对这个问题的思考不够深入。

您需要想出一种方法来大幅缩小这些数字,以便您仍然可以对它们进行比较。换句话说,您可能想要研究一种比较结果有多少位的方法。

提示是 log(a^b) = b * log(a)

You're under-thinking this problem.

You need to come up with a way to scale down these numbers drastically so you can still compare them. In other words, you may want to look into a way of comparing how many digits the result will be.

A hint would be log(a^b) = b * log(a)

下雨或天晴 2024-12-06 23:31:41

一个 32 位 int 只能容纳 2^32 个值,其中一些值在某些时候会神奇地变成负数......

A 32-bit int can only hold 2^32 values, and some of those magically turn negative at some point...

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