• 整数幂


     整数幂(多实例测试)

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 1073  Solved: 610

    Description

    求A^B的最后三位数表示的整数(1<=A,B<=1000)

    Input

    n个测试实例,每个实例给出两个正整数A,B

    Output

    输出A^B的最后三位(没有前导0)

    Sample Input

    2
    2 3
    12 6

    Sample Output

    8
    984
     
     

    Code:

     1 import java.util.Scanner;
     2 import java.math.BigInteger;
     3 public class Main{
     4 
     5     public static void main(String[] args) {
     6         Scanner input=new Scanner(System.in);
     7         int n=input.nextInt();
     8         while(n-->=0){
     9             BigInteger A=input.nextBigInteger();
    10             int B=input.nextInt();
    11             A=A.pow(B);              //将求得的大数A的B次幂赋值给A;
    12             int len=A.toString().length(); //将大数A转换成String型求其长度.
    13             if(len<=3)
    14                 System.out.println(A.intValue());  //若A长度小于3 则利用intValue()方法将A转化为int型.
    15             else{
    16                 A=new BigInteger(A.toString().substring(len-3,len));  //截取大数A中后三位数字
    17                 System.out.println(A.intValue());      //利用intValue()方法将A转化为int型.
    18             }
    19         }
    20     }
    21 }
  • 相关阅读:
    数组类型
    约瑟夫环问题
    const在c和c++中地位不同
    Makefile学习之路——4
    单链表综合操作
    算法初探——大O表示法
    数据结构实用概念
    Makefile学习之路——3
    翻转字符串
    Makefile学习之路——2
  • 原文地址:https://www.cnblogs.com/HRuinger/p/4009188.html
Copyright © 2020-2023  润新知