通过平方求幂(Project Euler 99) 关于我的解决方案的提示
这就是我正在谈论的问题 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你对这个问题的思考不够深入。
您需要想出一种方法来大幅缩小这些数字,以便您仍然可以对它们进行比较。换句话说,您可能想要研究一种比较结果有多少位的方法。
提示是 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)
一个 32 位 int 只能容纳 2^32 个值,其中一些值在某些时候会神奇地变成负数......
A 32-bit int can only hold 2^32 values, and some of those magically turn negative at some point...