数値の整数の次数を求めます(シミュレーションはpow関数を実現します)
数値の整数の次数を求めます:
double Power(double base,int exponent)を実現して、baseのponent次数を求めます.ライブラリ関数は使用できませんが、大きな数の問題は考慮されません.
簡単な解法だと思っています.
考慮:入力した指数(exponent)は1未満、つまりゼロと負数はどうしますか?明らかに上記の状況はこれらの状況を考慮していない.
最適化:
double Power(double base,int exponent)を実現して、baseのponent次数を求めます.ライブラリ関数は使用できませんが、大きな数の問題は考慮されません.
簡単な解法だと思っています.
double Power(double base, int exponent)
{
double result = 0.0;
for (int i = 1; i <= exponent; ++i)
{
result *= i;
}
return result;
}
考慮:入力した指数(exponent)は1未満、つまりゼロと負数はどうしますか?明らかに上記の状況はこれらの状況を考慮していない.
最適化:
double Power(double base, int exponent)
{
if (exponent ==0)
{
return 1;
}
if (exponent ==1)
{
return base;
}
int result = Power(base, exponent >> 1);
result *= result;
if (exponent & 0x01)
result *= base;
return result;
}