这道题就是HDOJ的1061的变形:
1061 :求n的n次方的个位数 http://www.cnblogs.com/xiezie/p/5596779.html
1097 :求n的m次方的个位数
因此,就不在这里赘述了
以下是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)); while(scan.hasNext()){ int n = scan.nextInt(); int m = 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((m-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-01 04:26:58