ACM中Java的使用

ACM中Java的使用

类名必须为Main,即:public class Main{}

基本语法

输入

方法一:(更快)

Scanner sc = new Scanner(new BufferedInputStream(System.in));

方法二:

Scanner sc = new Scanner(System.in);

读取数据

sc.hasNextInt();    //判断是否还有待读取的整数
int a = sc.nextInt();   //读取一个整数
sc.hasNextDouble();     //判断是否还有待读取的实数
double b = sc.nextDouble();     //读取一个实数
sc.hasNext();       //判断是否还有待读取的字符串
String c = sc.next();   //读取一个字符串(读取到空字符或行未结束)
sc.hasNextLine();   //判断是否还有待读取的字符串
String d = sc.nextLine();   //读取一个字符串(读取到行未结束)
//java中没有读取一个字符的方法,因此先将其当成字符串读取,在将字符取出
char e;
String ee = sc.nextLine();
e = ee.charAt(0);

输出

System.out.print();      // 输出后不换行,类似于 cout << … ;
System.out.println();    // 输出后换行,类似于 cout << … << endl;
System.out.printf();     // 类似于C中的printf

Java中的BigInteger类的使用

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
    //常量
        BigInteger ONE = BigInteger.ONE;
        BigInteger TEN = BigInteger.TEN;
        BigInteger ZERO = BigInteger.ZERO;
    //获得数据的方法
        //(1)直接读取键盘输入
        BigInteger aBig = null; 
        aBig = sc.nextBigInteger();
        //(2)将int等基本类型的数据转换成大数
        int b;
        b = sc.nextInt();
        BigInteger bBig = BigInteger.valueOf(b);
        //(3)将字符串类型的数据转换成大数
        BigInteger cBig = new BigInteger("10");
    //常见计算
        BigInteger resBig = null;   //存储结果
        //(1)计算和
        resBig = aBig.add(bBig);
        System.out.println(aBig + " + " + bBig + " = " + resBig);
        //(2)计算差
        resBig = aBig.subtract(bBig);
        System.out.println(aBig + " - " + bBig + " = " + resBig); 
        //(3)计算乘积
        resBig = aBig.multiply(bBig);
        System.out.println(aBig + " * " + bBig + " = " + resBig);  
        //(4)计算商
        resBig = aBig.divide(bBig);
        System.out.println(aBig + " / " + bBig + " = " + resBig);
        //(5)计算余数
        resBig = aBig.remainder(bBig);
        System.out.println(aBig + " % " + bBig + " = " + resBig);
        //(6)将商和余数存在数组中  
        BigInteger[] resarrBig = aBig.divideAndRemainder(bBig);  
        System.out.println("resarrBig:" + Arrays.toString(resarrBig));  
        System.out.println("resarrBig[0]:" + resarrBig[0]);  
        System.out.println("resarrBig[1]:" + resarrBig[1]);
    //其他计算
        //(1)取膜运算
        resBig = aBig.mod(bBig);
        System.out.println(aBig + " % " + bBig + " = " + resBig);
        //(2)取绝对值
        resBig = aBig.abs();
        System.out.println("|" + aBig + "|" + " = " + resBig);
        //(3)按位与
        resBig = aBig.and(bBig);
        System.out.println(aBig + " & " + bBig + " = " + resBig);
        //(4)做 &~ 运算
        resBig = aBig.andNot(bBig);
        System.out.println(aBig + " &~ " + bBig + " = " + resBig);
        //(5)清除指定位
        resBig = aBig.clearBit(2);
        System.out.println("将 " + aBig + " 的第2号位置零" + " = " + resBig);
        //(6)翻转指定位 (this ^ (1<<n))
        resBig = aBig.flipBit(2);
        System.out.println("将 " + aBig + " 的第2号位翻转" + " = " + resBig);
        //(7)计算最大公约数
        resBig = aBig.gcd(bBig);
        System.out.println(aBig + " 与 " + bBig + " 的最大公约数" + " = " + resBig);
        //(8)计算最大值
        resBig = aBig.max(bBig);
        System.out.println("max(" + aBig + ", " + bBig + ") = " + resBig);
        //(9)计算最小值
        resBig = aBig.min(bBig);
        System.out.println("min(" + aBig + ", " + bBig + ") = " + resBig);
        //(10)计算 (this-1 mod m)
        //异常:ArithmeticException - 如果 m ≤ 0, 或此BigInteger没有乘法逆模m(即,此BigInteger不是互质到m)。
        //resBig = aBig.modInverse(bBig);
        //System.out.println(aBig + "-1 % " + bBig + " = " + resBig);
        //(11)计算 A^B mod C
        resBig = aBig.modPow(bBig, cBig);
        System.out.println("(" + aBig + " ^ " + bBig + ") % " + cBig + " = " + resBig);
        //(12)计算相反数
        resBig = aBig.negate();
        System.out.println("-" + aBig + " = " + resBig);
        //(13)大于此数的第一个可能的素整数
        resBig = aBig.nextProbablePrime();
        System.out.println("大于 " + aBig + "的第一个素整数为 " + resBig);
        //(14)按位取反运算
        resBig = aBig.not();
        System.out.println("~" + aBig + " = " + resBig);
        //(15)按位或运算
        resBig = aBig.or(bBig);
        System.out.println(aBig + " | " + bBig + " = " + resBig);
        //(16)计算幂
        resBig = aBig.pow(2);
        System.out.println(aBig + "^2 " + " = " + resBig);
        //(17)返回一个正BigInteger可能是素数,以指定的bitLength
        Random rnd = new Random();
        resBig = aBig.probablePrime(3, rnd);
        System.out.println(aBig + " 以长度3返回素数为 " + resBig);
        //(18)将指定位置为1
        resBig = aBig.setBit(2);
        System.out.println("将 " + aBig + " 的第2号位置一" + " = " + resBig);
        //(19)左移位
        resBig = aBig.shiftLeft(2);
        System.out.println("将 " + aBig + " 左移2位" + " = " + resBig);
        //(20)右移位
        resBig = aBig.shiftRight(2);
        System.out.println("将 " + aBig + " 右移2位" + " = " + resBig);
        //(21)按位异或运算
        resBig = aBig.xor(bBig);
        System.out.println(aBig + " ^ " + bBig + " = " + resBig);
        //(22)转换成任意进制表示
        System.out.println("10进制的 " + aBig + " 为 " + aBig.toString());
        System.out.println("2进制的 " + aBig + " 为 " + aBig.toString(2));
        System.out.println("8进制的 " + aBig + " 为 " + aBig.toString(8));
        System.out.println("16进制的 " + aBig + " 为 " + aBig.toString(16));
        //(23)最小二进制补码表示的位的个数
        System.out.println(aBig + " 的最小二进制补码表示的位的个数为 " + aBig.bitLength());
        //(24)二进制补码表示中不包扩符号位的位的个数
        System.out.println(aBig + " 的二进制补码表示中不包扩符号位的位的个数为 " + aBig.bitCount());
        //(25)判断是否可能是素数,若返回true,则是素数的概率超出 (1 - 1/2certainty)
        int certainty = 10;
        if(aBig.isProbablePrime(certainty))
            System.out.println(aBig + " 可能是素数");
        else
            System.out.println(aBig + " 不是素数");
        //(26)转换成int
        System.out.println(aBig.intValue());
        //(27)转换成long
        System.out.println(aBig.longValue());
        //(28)转换成float
        System.out.println(aBig.floatValue());
        //(29)转换成double
        System.out.println(aBig.doubleValue());
    //比较运算
        int comp = aBig.compareTo(bBig);
        if(comp == 0)
            System.out.println(aBig + " = " + bBig);
        else if(comp > 0)
            System.out.println(aBig + " > " + bBig);
        else
            System.out.println(aBig + " < " + bBig);
    }
}

Java中的BigDecimal类的使用

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
    //常量
        BigDecimal ONE = BigDecimal.ONE;
        BigDecimal Ten = BigDecimal.TEN;
        BigDecimal ZERO = BigDecimal.ZERO;
    //获得数据的方法
        //(1)直接读取键盘输入
        BigDecimal aBig = null; 
        aBig = sc.nextBigDecimal();
        //(2)将int等基本类型的数据转换成大数
        double b;
        b = sc.nextDouble();
        BigDecimal bBig = BigDecimal.valueOf(b);
        //(3)将字符串类型的数据转换成大数
        BigDecimal cBig = new BigDecimal("11.1");
    //常见计算
        BigDecimal resBig = null;
        //(1)加
        resBig = aBig.add(bBig);
        System.out.println(aBig + " + " + bBig + " = " + resBig);
        //(2)减
        resBig = aBig.subtract(bBig);
        System.out.println(aBig + " - " + bBig + " = " + resBig);
        //(3)乘
        resBig = aBig.multiply(bBig);
        System.out.println(aBig + " * " + bBig + " = " + resBig);
        //(4)除 (需注意除不尽时应使用一下写法来设定精度)
        resBig = aBig.divide(bBig, 2, BigDecimal.ROUND_HALF_UP);
        System.out.println(aBig + " / " + bBig + " = " + resBig);
    //其他计算
        //(1)计算相反数
        resBig = aBig.negate();
        System.out.println(aBig + " 的相反数为 " + resBig);
    //设置精度,以精度为小数点后2位为例
        //(1)ROUND_DOWN:直接去掉多余的位数
        System.out.println(aBig + " 直接去掉多余的位数 " + aBig.setScale(2, BigDecimal.ROUND_DOWN));
        //(2)ROUND_UP:直接进位
        System.out.println(aBig + " 直接进位 " + aBig.setScale(2, BigDecimal.ROUND_UP));
        //(3)ROUND_CEILING:正数进位,负数舍去
        System.out.println(aBig + " 正数进位(CEILING) " + aBig.setScale(2, BigDecimal.ROUND_CEILING));
        System.out.println(aBig.negate() + " 负数舍去(CEILING) " + aBig.negate().setScale(2, BigDecimal.ROUND_CEILING));
        //(4)ROUND_FLOOR:正数舍去,负数进位
        System.out.println(aBig + " 正数舍去(FLOOR) " + aBig.setScale(2, BigDecimal.ROUND_CEILING));
        System.out.println(aBig.negate() + " 负数进位(FLOOR) " + aBig.negate().setScale(2, BigDecimal.ROUND_CEILING));
        //(5)ROUND_HALF_UP:四舍五入,当>=5时入
        System.out.println(aBig + " 四舍五入,当>=5时入 " + aBig.setScale(2, BigDecimal.ROUND_HALF_UP));
        //(6)ROUND_HALF_DOWN:四舍五入,当>5时入
        System.out.println(aBig + " 四舍五入,当>5时入 " + aBig.setScale(2, BigDecimal.ROUND_HALF_DOWN));
        //(7)ROUND_HALF_EVEN:四舍六入五成双(使要舍或入的5前面的数是偶数)
        System.out.println(aBig + " 四舍六入五成双 " + aBig.setScale(2, BigDecimal.ROUND_HALF_EVEN));
        //(8)ROUND_UNNECESSARY:用于断言请求的操作具有精确结果的舍入模式,因此不需要舍入 (默认模式)
        //System.out.println(aBig + " 断言请求的操作具有精确结果的舍入模式 " + aBig.setScale(2, BigDecimal.ROUND_UNNECESSARY));
    //转换
        //(1)转为字符串
        System.out.println(aBig.toString());
        //(2)转为int
        System.out.println(aBig.intValue());
        //(3)转为long
        System.out.println(aBig.longValue());
        //(4)转为float
        System.out.println(aBig.floatValue());
        //(5)转为double
        System.out.println(aBig.doubleValue());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值