整数幂(多实例测试)

来源:互联网 发布:数据安全防护体系 编辑:程序博客网 时间:2024/05/16 07:15

  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 }

  

复制代码
0 0