HDOJ 1061 Rightmost Digit(循环问题)

Problem Description
Given a positive integer N, you should output the most right digit of N^N.

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).

Output
For each test case, you should output the rightmost digit of N^N.

Sample Input
2
3
4

Sample Output
7
6

题意:很简单,就是输出n^n的最后一个数字时什么。

思路:前面有过一个0-9的n次方的题目,HDOJ1097题,那一题中我用代码推出了循环节,这个题目,我用的循环节全为4了.
HDOJ1097题博客链接:http://blog.csdn.net/qq_26525215/article/details/50949847

import java.util.Scanner;

public class Main{
    static long db[][] = new long[10][4];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        dabiao();
//      System.out.println(db[4][0]);
//      System.out.println(db[4][3]);
//
        long t = sc.nextLong();
        while(t-->0){
            int n = sc.nextInt();
            int f=n%10;
            int m=n%4;
            //System.out.println(m);
            m--;
            if(m<0){
                m=3;
            }
            System.out.println(db[f][m]);

        }
    }

    private static void dabiao() {
        for(int i=1;i<=9;i++){
            for(int j=0;j<4;j++){
                db[i][j]=dabiao(i,j);
                //System.out.print(db[i][j]+" ");
            }
            //System.out.println();
        }
    }

    private static long dabiao(long i, long j) {
        long m=i;//4,3
        for(int k=1;k<=j;k++){
            m=(m*i)%10;
        }
        m=m%10;
        return m;
    }

}
时间: 2024-07-31 18:42:33

HDOJ 1061 Rightmost Digit(循环问题)的相关文章

hdu 1061 Rightmost Digit

      和上一题一样的,只是最右边的数.找规律,20个一循环   import java.util.Scanner; public class Main { final static int a[]={0,1,4,7,6,5,6,3,6,9,0,1,6,3,6,5,6,7,4,9}; static Scanner cin=new Scanner(System.in); static int n,T; public static void main(String[] args) { T=cin

HDOJ 1097 A hard puzzle(循环问题)

Problem Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin. this puzzle describes that: gave a and b,how to know

HDOJ(HDU) 1799 循环多少次?(另类杨辉三角)

Problem Description 我们知道,在编程中,我们时常需要考虑到时间复杂度,特别是对于循环的部分.例如, 如果代码中出现 for(i=1;i<=n;i++) OP ; 那么做了n次OP运算,如果代码中出现 fori=1;i<=n; i++) for(j=i+1;j<=n; j++) OP; 那么做了n*(n-1)/2 次OP 操作. 现在给你已知有m层for循环操作,且每次for中变量的起始值是上一个变量的起始值+1(第一个变量的起始值是1),终止值都是一个输入的n,问最后

unix-shell脚本执行异常,一直循环

问题描述 shell脚本执行异常,一直循环 脚本目的,分析脚本执行结果,将有问题的项目附加到结果后面. #!/bin/bash function analyse_report() { abnormal_item=$1 IP=$2 case "$abnormal_item" in 3) echo "hostname of $IP">>Linux_1410221605.txt cat ../result/1410231618/result_192.168.5

2014秋C++ 第9周项目 循环程序设计

课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703,课程资源在云学堂"贺老师课堂"同步展示,使用的帐号请到课程主页中查看. 阅读程序 程序分析题,阅读下列程序,写出程序的运行结果,建议在上机时进行验证(云学堂将给出代码,直接复制到C4droid或CodeBlocks中运行即可),如果与自己的预期有出入,尤其注意对照找出问题. 读这些小程序,可以见识不少处理技巧.读程序,也是一种非常非常重要的学习方式,应该给予重视!

HDOJ 1164 Eddy&amp;#39;s research I(拆分成素数因子)

Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can't write program, so Eddy has to ask intelligent you to h

C语言循环结构与时间函数用法实例教程_C 语言

本文实例展示了C语言循环结构与时间函数用法,对于C语言的学习来说是非常不错的参考借鉴材料.分享给大家供大家参考之用.具体如下: 完整实例代码如下: /********************************************** ** <Beginning C 4th Edition> Notes codes ** Created by Goopand ** Compiler: gcc 4.7.0 *****************************************

php对关联数组循环遍历的实现方法

 这篇文章主要介绍了php对关联数组循环遍历的实现方法,涉及php操作数组的技巧,具有一定参考借鉴价值,需要的朋友可以参考下     本文实例讲述了php对关联数组循环遍历的实现方法.分享给大家供大家参考.具体分析如下: php对于类似 ? 1 $age = array("zhangshan"=>14,"lisi"=>15,"sharejs"=>16); 这样的数组可以通过foreach的方法进行遍历,下面是详细的代码: ? 1

viewpager循环滚动和自动轮播的问题

ViewPager是一个常用的android组件,不过通常我们使用ViewPager的时候不能实现左右无限循环滑动,在滑到边界的时候会看到一个不能翻页的动画,可能影响用户体验.此外,某些区域性的ViewPager(例如展示广告或者公告之类的ViewPager),可能需要自动轮播的效果,即用户在不用滑动的情况下就能够看到其他页面的信息. 为此我查阅了网络上现有的一些关于实现这样效果的例子,但都不是很满意,经过反复实验,在这里总结并分享给大家,希望能有所帮助. 循环滑动效果的实现:PagerAdap