被験者11の整数電力値

////////////////////////////////////////////////// ///////////////////////////////////
// 1.タイトル整数電力値11

// 1.1陰性とみなされ、0 !!!!ない
ダブルパワー(ダブル dBaseの、INT iExponent)
{ 
    ダブル DRES = 1.0 ;
     のためint型 I = 0 ; IはiExponentを<Iは++ 
    { 
        DRES * = dBaseの; 
    } 

    リターンDRES; 
} 

//は二つの等しい二重か否かを判断する
BOOL等しい(ダブル LHS、ダブルRHS)
{ 
    がiF((LHS - RHS> - 0.0000001)&&(LHS - RHS < 0.0000001 ))
    {
        返す ; 
    } 

    を返す 
} 

ダブル PowerWithUnsignedExponent(ダブル dBaseの、符号なし整数uiExponent)
{ 
    ダブル dResult = 1.0 (符号なし整数 I = 1 ; I <= uiExponent; iは++ 
    { 
        dResult * = dBaseの。
    } 

    戻りdResult。
} 

// 1.2全面但不够高效的算法
ダブル POWER_1(二重のdBase、INT iExponent)
{
    二重 dResult = 0.0 もし(等号(dBaseの、0.0)&& iExponent < 0 
    { 
        リターンdResult。
    } 

    符号なし整数 absExponent =(符号なし整数)iExponent。
    もし(iExponent < 0 
    { 
        absExponent =(符号なし整数( - )iExponent)。
    } 

    DResult = PowerWithUnsignedExponent(dBaseの、absExponent)。
    もし(iExponent < 0 
    { 
        dResult= 1.0 / dResult; 
    } 

    戻りdResultを; 
} 

// 1.3包括的かつ効率的なソリューション
 // EG入力iExponent = 32、PowerWithUnsignedExponentループ31回必要な場合
 // もう一つのアプローチは、(a)32 - >( (A )16)2 - >(( (A)8)2)2 - > ... - >(((((A)2)2)2)2)2 - > 5つのラン

ダブル NewPowerWithUnsignedExponent(ダブル dBaseの、符号なし整数uiExponent)
{ 
    IF0 == )uiExponent 
    { 
        戻り 1.0 ; 
    } 
    そう IF1 == uiExponent)
    { 
        リターンdBaseの。
    } 

    ダブル dResult = NewPowerWithUnsignedExponent(dBaseの、uiExponent >> 1 )。
    dResult * = dResult。

    // 如果是基数
    場合(uiExponent&0×01 
    { 
        dResult * = dBaseの。
    } 

    戻りdResult。
} 

ダブル Power_2(二重のdBase、INT iExponent)
{ 
    ダブル dResult = 0.0 もし(等号(dBaseの、0.0)&& iExponent < 0 
    { 
        リターンdResult; 
    } 

    符号なし整数 absExponent =(符号なし整数)iExponent。
    もし(iExponent < 0 
    { 
        absExponent =(符号なし整数( - )iExponent)。
    } 

    DResult = NewPowerWithUnsignedExponent(dBaseの、absExponent)。
    もし(iExponent < 0 
    { 
        dResult = 1.0 / dResult。
    } 

    戻りdResult。
} 

ボイドPowerTestFunc()
{ 
    COUT <<" \ N \ N --------------- PowerTestFuncスタート--------------> " << てendl;
    INT aiArray [] = { - 3、 - 2、 - 10123456 }。
    INT ILEN = はsizeof(aiArray)/ はsizeofINT )。
    ダブルのdBase = 5 ; 
    
    裁判所未満 << " 方法一:========================== >> " << てendl;
    以下のためにint型 i = 0 ; iはILENを<; iは++ 
    { 
        のprintf(" [%のLF]的%D次方:%LFを\ n " 、dBaseの、aiArray [i]は、電源(dBaseの、aiArray [I])) ; 
    } 

    coutの << " 方法二:========================== >> " << てendl;
    以下のためにint型 i = 0 ; iはILENを<; iは++ 
    { 
        のprintf(" [%のLF]的%D次方:%LFを\ n " 、dBaseの、aiArray [i]は、POWER_1(dBaseの、aiArray [I])) ; 
    } 

    coutの << "方法三:========================== >> " << ENDL;
     のためint型 i = 0 ; iはILENを<; iは++ 
    { 
        printf(" [%のLF]的%D次方:%LFを\ n " 、dBaseの、aiArray [i]は、Power_2(dBaseの、aiArray [I])); 
    } 

    COUT << " \ n \ nは---- ----------- PowerTestFunc終了--------------> " << てendl; 

}


おすすめ

転載: www.cnblogs.com/yzdai/p/11258684.html