zoukankan      html  css  js  c++  java
  • Java代码之"求a的N次方"

    二分法:

    package com.test;

    public class Power {

        public static void main(String[] args) {
            System.out.print("Hello, \r\n");
            
            System.out.print(power2(3, 2) + "\r\n");
            System.out.print(power(3, 2) + "\r\n");
            
            System.out.print(power2(3, 3) + "\r\n");
            System.out.print(power(3, 3) + "\r\n");
            
            System.out.print(power2(3, 32) + "\r\n");
            System.out.print(power(3, 32) + "\r\n");
        }

        static long power(long a, int n) {
            long r = 1;
            int t = 0;
            while (n >= 1) {
                if ((n & 1) == 1) {
                    r *= a;
                    t++;
                }
                a *= a;
                t++;
                n = n >> 1;
            }
            System.out.print(t + "times \r\n");
            return r;
        }
        
        static long power2(long a, int n) {
            long r = 1;
            while (n-- >= 1) {
                r =r *  a;
            }
            return r;
        }
    }

    id 博主 = [[KILONET.CNBLOGS.COM alloc] initWithValue:@"天堂向右,我依然向左"

                  网名:@"老舟"

                  兴趣:@"影音,阅读"

                  动态:@"系统架构设计,Android通信模块开发"

                  网址:@"http://kilonet.cnblogs.com"
                  签名:@"--------------------------------------------------

                                  Stay Hungry , Stay Foolish

                                  求  知  若  渴,处  事  若  愚

                              --------------------------------------------------"

                  ];         // Never Release

  • 相关阅读:
    Vue之数据排序加签
    微信小程序之评分页面
    Vue之展示PDF格式的文档
    动态规划问题思考(DP)
    LitJson的使用
    c#事件管理器
    unity shader 学习
    unity ugui图片自适应文字内容大小
    unity3d各种OpenFileDialog操作
    ue4 使用3dsmax制作布料的插件及下载位置
  • 原文地址:https://www.cnblogs.com/KiloNet/p/2418681.html
Copyright © 2011-2022 走看看