[ Algorithm ] N次方算法 N Square 动态规划解决

public static decimal NSquare(decimal number, int square)
{

    if (square < 0)
    {
        square = -square;
        number = 1 / number;
    }

    decimal result = 1;

    while (square > 0)
    {
        if (square % 2 == 1) result *= number;
        number *= number;
        square /= 2;
    }
    return result;
}

 

posted @ 2013-07-16 01:29  程序员文道  阅读(421)  评论(0编辑  收藏  举报