首页 > 其他 > 详细

HDU - 1061-快速幂签到题

时间:2018-11-24 20:19:23      阅读:154      评论:0      收藏:0      [点我收藏+]

快速幂百度百科:快速幂就是快速算底数的n次幂。其时间复杂度为 O(log?N), 与朴素的O(N)相比效率有了极大的提高。

HDU - 1061

代码实现如下:

import java.util.Scanner;

public class Main
{
    public static void main(String []args)
    {
        Scanner cin = new Scanner(System.in);
        int T = cin.nextInt();
        for(int i = 0; i < T; i++)
        {
            int N = cin.nextInt();
            System.out.println(Search(N));
        }
    }
    static int Search(int N)//快速幂的模板一般都与下面的代码相同
    {
        int ans = 1;
        int temp = N;
        while(N != 0)
        {
            if((N & 1) != 0)
            {
                ans = (ans%10)*(temp%10);
            }
            temp = (temp%10) * (temp%10);
            N = N>>1;
        }
        ans = ans%10;
        return ans;
    }
}

 

HDU - 1061-快速幂签到题

原文:https://www.cnblogs.com/674001396long/p/10013223.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!