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

  • 相关阅读:
    #include "stdafx.h" 错误?
    扩频技术
    求数组中只出现一次的数字(算法)
    1.3一摞烙饼的排序
    嵌套类
    企业级邮件服务软件推荐
    关于Linq To Sql中Detach方法和一个公共基类
    asp.net(c#) 将dbf转换为xls或wps,并将数据的列名改成中文;并判断本机是否安装office2003,2007和wps2007,2010
    一句代码解决IE8兼容问题(兼容性视图)
    asp.net(C#)套用模板操作Excel
  • 原文地址:https://www.cnblogs.com/KiloNet/p/2418681.html
Copyright © 2011-2022 走看看