zoukankan      html  css  js  c++  java
  • JZ012数值的整数次方

    数值的整数次方

    题目描述

    给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

    • 保证base和exponent不同时为0。

    题目链接: 数值的整数次方

    代码

    /**
     * 标题:数值的整数次方
     * 题目描述
     * 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
     * 保证base和exponent不同时为0
     * 题目链接:
     * https://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00?tpId=13&&tqId=11165&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
     */
    public class Jz12 {
    
      public double Power(double base, int exponent) {
        double result = 0;
        if (exponent > 0) {
          result = base;
          while (exponent > 1) {
            result *= base;
            exponent--;
          }
        } else {
          result = 1;
          while (exponent <= -1) {
            result /= base;
            exponent++;
          }
        }
        return result;
      }
    
      public static void main(String[] args) {
        Jz12 jz12 = new Jz12();
        System.out.println(jz12.Power(2, 3));
        System.out.println(jz12.Power(2, -3));
      }
    
    }
    

    【每日寄语】 时间永远不会逆行,把握好每一个属于自己的清晨。

  • 相关阅读:
    sws_getContext函数参数介绍
    FFmpeg 将YUV数据转RGB
    信号槽同步
    git stash
    Qt 事件
    Qt 信号与槽函数
    微信红包的算法实现
    Python正则表达式中的re.S
    hive函数 -- split 字符串分割函数
    python None与Null
  • 原文地址:https://www.cnblogs.com/kaesar/p/15627719.html
Copyright © 2011-2022 走看看