前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDOJ 1097 A hard puzzle(循环问题)

HDOJ 1097 A hard puzzle(循环问题)

作者头像
谙忆
发布2021-01-21 14:20:13
3080
发布2021-01-21 14:20:13
举报
文章被收录于专栏:程序编程之旅

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< a,b<=2^30)

Output For each test case, you should output the a^b’s last digit number.

Sample Input 7 66 8 800

Sample Output 9 6

本题重要的是循环节的判断,java的大数会超时的。 下面代码实现了循环节的寻找。

代码语言:javascript
复制
import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    static int da[] = new int[10];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        dabiao();//打表
        while(sc.hasNext()){
            //超时
//          BigDecimal a = sc.nextBigDecimal();
//          int b = sc.nextInt();
//          a = a.pow(b);
//          String str = a.toString();
//          System.out.println(str.charAt(str.length()-1));

            //找规律
            int a = sc.nextInt();
            int b = sc.nextInt();
            a = a%10;
            switch(a){
            case 0:System.out.println(da[0]);break;
            case 1:System.out.println(da[1]);break;
            case 2:System.out.println(shuchu(b,da[2],2));break;
            case 3:System.out.println(shuchu(b,da[3],3));break;
            case 4:System.out.println(shuchu(b,da[4],4));break;
            case 5:System.out.println(shuchu(b,da[5],5));break;
            case 6:System.out.println(shuchu(b,da[6],6));break;
            case 7:System.out.println(shuchu(b,da[7],7));break;
            case 8:System.out.println(shuchu(b,da[8],8));break;
            case 9:System.out.println(shuchu(b,da[9],9));break;
            }

        }

    }

    private static int shuchu(int b, int i, int j) {
        b=b%i;
        int sum=j;
        if(b==0){
            b=i;
        }
        for(int k=1;k<b;k++){
            sum=sum*j;
        }
        return sum%10;
    }



    private static void dabiao() {
        da[0]=0;
        da[1]=1;

        int h=0;
        for(int i=2;i<10;i++){
            h=0;
            for(int k=2;k<10;k++){
                if(i==hm(k,i)){
                    h=k-1;
                    break;
                }
            }
            da[i]=h;
        }

        //0-9的循环节输出
//      for(int i=0;i<10;i++){
//          System.out.println(da[i]);
//      }


    }

    private static int hm(int k,int i) {
        int sum=1;
        for(int j=0;j<k;j++){
            sum=sum*i;
        }
        return sum%10;
    }


}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/03/21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档