C++ 平方求幂(Euler 99项目)关于我的解决方案的提示
这就是我正在谈论的问题 我的代码将正确编译和运行。我猜是计算出了问题的症结所在。它告诉我633号线是最大的,欧拉项目说这是不正确的C++ 平方求幂(Euler 99项目)关于我的解决方案的提示,c++,computation,C++,Computation,这就是我正在谈论的问题 我的代码将正确编译和运行。我猜是计算出了问题的症结所在。它告诉我633号线是最大的,欧拉项目说这是不正确的 #include <iostream> #include <string> #include <fstream> using namespace std; int poww(int base, int exp); int main() { //ignore messy/unused variables. I am d
#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;
}
你正在考虑这个问题 你需要想出一种方法来大幅缩小这些数字,这样你仍然可以比较它们。换句话说,您可能需要研究一种比较结果的位数的方法
一个提示是loga^b=b*loga一个32位int只能保存2^32个值,其中一些值在某个点神奇地变为负值…您需要定义不正确的。@tedled?什么是不正确的?它不能编译吗?它崩溃了?它工作正常,但不正常?你开它的时候它会开火吗?请具体说明您的问题。已添加。我的程序说第633行是最好的。忽略混乱/未使用的变量很难…我知道。我真的非常恼火,只是想让它发挥作用。是的,这就是为什么我要使用平方求幂例程。不,关键是你需要一个更大的数字类型来存储答案。这并不能改变整型不能容纳300万位数字的事实。你需要想出一种方法来大幅缩小这些数字,这样你仍然可以比较它们。换言之,您可能需要研究一种方法来比较结果的位数。一个int最多只能容纳2^32位,甚至远未接近300万位。研究涉及对数的解决方案。这个问题的关键是,你的解决方案必须比较这些指数的结果,而不是实际计算它们。估计位数不是比计算10**x=a**b中的x多了一步吗?正在接收的位数为+1?