質問
入力ファイルには、1≦n、m <100の間の2つの数字があります。 電源NにMを表示する必要があります。Pow(X、Y)機能を使用すると、EX :::12に大きな整数を計算することはできません。誰かがこの演習を解決することができますか?
解決
これは外部ライブラリを使用せずにはそれほど難しくありません。ベクトル内の数字の桁数を記憶し、数字で桁数を算出します(紙のように)
Example:
power(12,23):
Store as start->1->2->end
step 1 result: start->1->4->4->end
step 2 result: start->1->7->2->8->end
and so on...
. 他のヒント
あなたはあなたのソースコードを見せることができますか? doubleを使用すると結果を保存するのに十分です 以下はあなたが参考の私のテストコードです:
#include <iostream>
#include <cmath>
using namespace std;
int main (int argc, char *argv[])
{
double result = pow (12, 23);
cout.precision (26);
cout << "Result: " << result << endl;
}
. Try to store your base in a long double
before calling pow :
long double base = 12;
long double result = pow(base, 23);
It is not required since C++11 though, you can get a good approximation like this :
#include <iomanip>
#include <iostream>
#include <cmath>
int main ()
{
std::cout << std::fixed << std::setprecision(0) << pow(99, 99) << std::endl;
}
Output :
369729637649726802192985226395427290145296428445515959701359650120802601667133273280053721002700400354392780458116125965728631706472588849812738072765460822138161108630185181415759762204338929270784
But it is an approximation, for instance Python2
code print 99 ** 99
outputs this :
369729637649726772657187905628805440595668764281741102430259972423552570455277523421410650010128232727940978889548326540119429996769494359451621570193644014418071060667659301384999779999159200499899
To get an exact result in C++, you should look at some BigInt libraries.