星期三, 二月 17, 2016

面试题11:数值的整数次方

1、从3个方面确保代码的完整性

23种错误处理的方法

a、函数用返回值告知调用者是否出错

b、当发生错误时设置一个全局变量

c、异常

 

面试题11:数值的整数次方

 

/*题目描述

 

给定一个double类型的浮点数baseint类型的整数exponent。求baseexponent次方。*/

#include<iostream>

using namespace std;

class Solution {

public:

    double Power(double base, int exponent) {

double y=base;

double res=1;

if(exponent<0){

exponent*=-1;

y=1/y;

}

while(exponent){

if(exponent&1)res*=y;

y*=y;

exponent>>=1;

}

return res;

    }

};

//int main(){

//        Solution test=Solution();

//        cout<<test.Power(2,0);

//        system("pause");

//        return 0;

//}

 

 

已使用 Microsoft OneNote 2010 创建
一个用于存放所有笔记和信息的位置

没有评论:

发表评论