package com.demo2;
import java.util.Scanner;
/*
* 求A^B的最后三位数表示的整数。
说明:A^B的含义是“A的B次方”
*/
public class HDU_oj2035 {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
while (sn.hasNext()) {
int A = sn.nextInt() % 1000; // 先取模,使得数据变小
int B = sn.nextInt();
if (A == 0 && B == 0)
break;
int ans = 1;
while (B != 0) {
ans = ans * A; //次方
ans = ans % 1000;//一直取模相乘 与相乘结果做后取模,一致
B--;
}
System.out.println(ans);
}
sn.close();
}
}
顺便学了一些大数的知识点:
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.PriorityQueue;
import java.util.Queue;
public class Practice {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append(5);
sb.append("hello");
System.out.println(sb);
// 强大的反转 反转一切容器
sb.reverse();
System.out.println(sb);
// BigInteger类实现了任意精度的整数运算,BigDecimal类实现了任意精度的浮点运算
// 如果基本的整数和浮点数精度不能够满足需求,这两个类可以处理任意长度数字序列的数值
BigInteger a;
BigDecimal b;
Queue<Integer> que = new PriorityQueue<Integer>();
que.add(3);
que.add(5);
que.add(10);
que.add(7);
que.add(9);
que.add(15);
que.add(11);
System.out.println(que);
// 每一次都会自动建立小顶堆(堆排序)
// while(!que.isEmpty()) {
// System.out.print(que.poll()+ ",");
// }
int size = que.size(); //一定要事先先把size保存,因为每执行一步size都会减小一
for(int i = 0; i<size;i++) {
System.out.print(que.poll() + ",");
}
System.out.println();
System.out.println("*****************************************");
// 大数操作
/*
* BigInteger不是基本数据类型之一,它其实更像String,是Java里的一个类,
* 然而它的初始化方式却没有String那么方便可以直接赋值,
* 而是跟其他自定义的类一样,要调用它的构造器进行初始化。
* 这个类的取值范围原则上是没有上限的,取决于你的计算机的内存
*/
/*
* 这里面最好用的应该是BigInger(String val)这个构造器吧,
* 可以直接将十进制的字符串格式变成大整数,
* 举例: BigInteger a=new BigInteger(“2222222222222222”);
*
既然不是基本数据类型,所以大数的加减乘除也不能使用+、-、*、/这些运算符号,
Java也没有对这些运算符号进行重定义,取而代之的是用一些方法来代替,
比如add()、subtract()、mutiply()、divide()这四种方法
*/
BigInteger aa = new BigInteger("-1235555564446");
BigInteger bb = new BigInteger("6461364636");
System.out.println(aa.add(bb).toString());
//验证取模守恒定律
int testA = 1234521;
int testB = 45;
int ansA = testA * testB;
System.out.println(ansA % 1000);
int ansB = (testA%1000) * (testB%1000);
System.out.println(ansB % 1000); //最后再取模一次
//BigInteger整数
//BigDecimal小数,精度不会丢失
BigDecimal aa1 = new BigDecimal("0.1");
BigDecimal bb1 = new BigDecimal("0.2");
System.out.println(aa1.add(bb1).toString()); //0.3
/*
* 这是为什么呢?这是因为我们计算机在进行浮点运算时,采用的是二进制运算,这样做非常容易导致精度丢失.
*/
double aa2 = 0.1;
double bb2 = 0.2;
System.out.println(aa2 + bb2); //0.300000000004
}
}