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 the a^b’s the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.

Input
There are mutiple test cases. Each test cases consists of two numbers a and b(0

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int a=sc.nextInt();
            int b=sc.nextInt();
            int x=a%10;
            if(x==0||x==5||x==6||x==1){
                System.out.println(x);
            }else{
                int[] arr=new int[9];
                arr[0]=x;
                int flag=0;
                for(int i=1;i<9;i++){
                    arr[i]=(arr[i-1]*x)%10;
                    if(arr[i]==x){//找到个位循环出现的次数
                        flag=i;
                        break;
                    }
                }
                int f=b%flag-1;
                if(f<0){//注意,当f==0时的特殊情况
                    f+=flag;
                }
                System.out.println(arr[f]);
            }
        }
    }

}

你可能感兴趣的:(HDOJ 1097 A hard puzzle)