HDOJ-ACM1061(JAVA) Rightmost Digit

题意:求n的n次方的个位数(1<=N<=1,000,000,000)

第一个最愚蠢的办法就是暴力破解,没什么意义,当然,还是实现来玩玩。

以下是JAVA暴力破解:

import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(new BufferedInputStream(System.in));     int m = scan.nextInt();
        while(m--!=0){
            int n = scan.nextInt();
            double result = Math.pow(n, n);
            System.out.println(result%10);
        }
        scan.close();
    }
}

第二个思路是寻找规律:

规律:求取个位数,其实就等于 n的个位数的n次方,252^252 的个位数等于 2^252的个位数

  另外: 无论n为多少,他的个位数是1-9,那么如 2 * 2 = 4 ; 2*4=8 ; 2*8 = 16 ; 2*16 = 32 ; 当 结果为 32 个位数为 2 , 那便开始了新一轮的 循环(2,4,8,6,2,4....)。

以下是JAVA实现:

import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(new BufferedInputStream(System.in));
        int m = scan.nextInt();
        while(m--!=0){
            int n = scan.nextInt();
            int rightMostDigitOfN = n%10;
            ArrayList<Integer> rightMostDigits = arrays.get(rightMostDigitOfN);
            int size = rightMostDigits.size();
            if(size == 1){
                System.out.println(rightMostDigitOfN);
            }else{
                System.out.println(rightMostDigits.get((n-1)%size));
            }
        }
        scan.close();
    }

    static ArrayList<ArrayList<Integer>> arrays  = getRightMostDigitArray();

    static ArrayList<ArrayList<Integer>> getRightMostDigitArray(){
        ArrayList<ArrayList<Integer>> arrays = new ArrayList<>();
        for(int i = 0 ; i != 10 ; i ++){
            ArrayList<Integer> integers = new ArrayList<>();
            integers.add(i);
            int n = 2;
            while(true){
                int rightMostDigit = (int) (Math.pow(i, n)%10);
                if(rightMostDigit==i){
                    break;
                }else{
                    integers.add(rightMostDigit);
                }
                n++;
            }
            arrays.add(integers);
        }
        return arrays;
    }

}
时间: 2024-10-25 17:30:11

HDOJ-ACM1061(JAVA) Rightmost Digit的相关文章

HDOJ 1061 Rightmost Digit

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30543    Accepted Submission(s): 11624 Problem Description Given a positive integer N, you should output the most right digit of N

[HDOJ]Rightmost Digit

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 38642    Accepted Submission(s): 14558 Problem Description Given a positive integer N, you should output the most right digit of N

hdoj 1061 Rightmost Digit【快速幂求模】

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 39554    Accepted Submission(s): 14930 Problem Description Given a positive integer N, you should output the most right digit of N

HDU 1061 Rightmost Digit(找规律)

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 43732    Accepted Submission(s): 16434 Problem Description Given a positive integer N, you should output the most right digit of

HDU - 1061 - Rightmost Digit (快速幂取模!)

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 34329    Accepted Submission(s): 13091 Problem Description Given a positive integer N, you should output the most right digit of

hdu oj 1061 Rightmost Digit (快速幂算法)

这里首先要讲解一下快速幂算法: 快速幂取模算法 在网站上一直没有找到有关于快速幂算法的一个详细的描述和解释,这里,我给出快速幂算法的完整解释,用的是C语言,不同语言的读者只好换个位啦,毕竟读C的人较多~ 所谓的快速幂,实际上是快速幂取模的缩写,简单的说,就是快速的求一个幂式的模(余).在程序设计过程中,经常要去求一些大数对于某个数的余数,为了得到更快.计算范围更大的算法,产生了快速幂取模算法.[有读者反映在讲快速幂部分时有点含糊,所以在这里对本文进行了修改,作了更详细的补充,争取让更多的读者一目

Rightmost Digit

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 35128 Accepted Submission(s): 13337 Problem Description Given a positive integer N, you should output the most right digit of N^N. I

HDU Rightmost Digit

Rightmost Digit Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 87   Accepted Submission(s) : 38 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Given a positive inte

Rightmost Digit(杭电1061)(求N^N的最低位)

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 33558    Accepted Submission(s): 12831 Problem Description Given a positive integer N, you should output the most right digit of