用java来处理高精度问题,相信对很多ACMer来说都是一件很happy的事,简单易懂。
头文件:
import java.io.*;
import java.util.*;
import java.math.*;
读入:
Scanner cin = Scanner (System.in);
while(cin.hasNext())//等价于!=EOF
n=cin.nextInt();//读入一个int型的数
n=cin.nextBigInteger();//读入一个大整数
输出:
System.out.print(n);//打印n
System.out.println();//换行
System.out.printf("%d\n",n);//也可以类似c++里的输出方式
定义:
int i,j,k,a[];
a = new int[100];
BigInteger n;
BigDecimal m;
String s;
BigInteger中一些常见的函数:
A=BigInteger.ONE
B=BigInteger.TEN
C=BigInteger.ZERO
BigInteger.valueOf(k);
基本的函数:
使用方法例如:a.add(b);
add: +
subtract: -
multiply: *
divide: /
remainder: %
divideAndRemainder:a[0]=this / val; a[1]=this % val
pow:a.pow(b)=a^b
gcd,abs:公约数,绝对值
negate:取负数
signum:符号函数
mod:a.mod(b)=a%b;
shiftLeft:左移,this << n ,this*2^n;
shiftRight:右移,this >> n,this/2^n;
and:等同于c++的&&,且;
or:||,或;
xor:异或,BigInteger xor(BigInteger val),this^val
not:!,非;
isProbablePrime:如果该 BigInteger 可能是素数,则返回 true ;如果它很明确是一个合数,则返回 false 。 参数 certainty 是对调用者愿意忍受的不确定性的度量:如果该数是素数的概率超过了 1 - 1/2**certainty方法,则该方法返回 true 。执行时间正比于参数确定性的值。
compareTo:根据该数值是小于、等于、或大于 val 返回 -1、0 或 1;
equals:判断两数是否相等,也可以用compareTo来代替;
min,max:取两个数的较小、大者;
浮点数定点输出
import java.util.*;
import java.math.*;
import java.text.DecimalFormat;
public class Main {
public static void main(String args[])
{
Scanner cin=new Scanner(System.in);
double x1,y1,x2,y2;
DecimalFormat df = new DecimalFormat("0.00");
while(cin.hasNext())
{
x1=cin.nextDouble();
y1=cin.nextDouble();
x2=cin.nextDouble();
y2=cin.nextDouble();
double temp;
temp=Math.sqrt(((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
System.out.println(df.format(temp));
}
}
}