日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
數(shù)值的整數(shù)次方-創(chuàng)新互聯(lián)

題目描述: 實現(xiàn)函數(shù) double Power(double base, int exponent), 求base的exponent次方。不得使用庫函數(shù),同時不需要考慮大數(shù)問題。

站在用戶的角度思考問題,與客戶深入溝通,找到米東網(wǎng)站設(shè)計與米東網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋米東地區(qū)。

分析:

有的面試者可能認(rèn)為題目很簡單,因此順手就寫下了如下代碼:

double Power(double base, int exponent)
{
    double result = 1.0;
    for(int i = q; i <= exponent; ++i)
        result *= result;
        
        return result;
}

如果寫出這樣的代碼,那么請面壁思過!

全面但不夠高效的解法

數(shù)值的整數(shù)次方

數(shù)值的整數(shù)次方

bool g_InvalidInput = false;
double Power(double base, int exponent)
{
    g_InvalidInput = false;
 
    if(equal(base, 0.0) && exponent < 0)
    {
        g_InvalidInput = true;
        return 0.0;
    }
 
    unsigned int absExponent = (unsigned int)(exponent);
    if(exponent < 0)
        absExponent = (unsigned int)(-exponent);
 
    double result = PowerWithUnsignedExponent(base, absExponent);
    if(exponent < 0)
        result = 1.0 / result;
 
    return result;
}


double PowerWithUnsignedExponent(double base, unsigned int exponent)
{
    double result = 1.0;
    /
    for(int i = 1; i <= exponent; ++i)
        result *= base;
 
    return result;
}


bool equal(double num1, double num2)
{
    if((num1 - num2 > -0.0000001)
        && (num1 - num2 < 0.0000001))
        return true;
    else
        return false;
}

數(shù)值的整數(shù)次方

數(shù)值的整數(shù)次方

其實,上述解法已經(jīng)比較全面了,但是如果遇到追求效率的面試官,則會提醒你還有更高效的方法。

數(shù)值的整數(shù)次方

數(shù)值的整數(shù)次方

這個公式很容易使用遞歸來實現(xiàn)

double PowerWithUnsignedExponent(double base, unsigned int exponent)
{
    if(exponent == 0)
        return 1;
    if(exponent == 1)
        return base;

    double result = PowerWithUnsignedExponent(base, exponent >> 1);
    result *= result;
    if((exponent & 0x1) == 1)
        result *= base;

    return result;
}

數(shù)值的整數(shù)次方

數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方數(shù)值的整數(shù)次方

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。


網(wǎng)站欄目:數(shù)值的整數(shù)次方-創(chuàng)新互聯(lián)
鏈接分享:http://www.dlmjj.cn/article/ggpod.html