HDOJ 1061 Rightmost Digit(循环问题)

简介: 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

image.png


题意:很简单,就是输出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;
    }
}
目录
相关文章
LeetCode 233. Number of Digit One
给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数。
62 0
LeetCode 233. Number of Digit One
HDOJ 1197 Specialized Four-Digit Numbers
HDOJ 1197 Specialized Four-Digit Numbers
84 0
|
Java
HDOJ 1097 A hard puzzle(循环问题)
HDOJ 1097 A hard puzzle(循环问题)
97 0
HDOJ/HDU 2163 Palindromes(判断回文串~)
HDOJ/HDU 2163 Palindromes(判断回文串~)
74 0
|
Web App开发 Java 数据安全/隐私保护
HDOJ(HDU) 1563 Find your present!(异或)
HDOJ(HDU) 1563 Find your present!(异或)
216 0
|
存储
HDOJ/HDU 1073 Online Judge(字符串处理~)
HDOJ/HDU 1073 Online Judge(字符串处理~)
79 0
|
Go
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
88 0
HDOJ 1391 Number Steps(打表DP)
HDOJ 1391 Number Steps(打表DP)
101 0
HDOJ 1391 Number Steps(打表DP)
HDOJ1002题A + B Problem II,2个大数相加
HDOJ1002题A + B Problem II,2个大数相加
92 0
HDOJ/HDU 1062 Text Reverse(字符串翻转~)
HDOJ/HDU 1062 Text Reverse(字符串翻转~)
102 0