用java以正确的姿势刷CSP

您所在的位置:网站首页 csp考试用什么编译器 用java以正确的姿势刷CSP

用java以正确的姿势刷CSP

2024-02-12 13:01| 来源: 网络整理| 查看: 265

许多程序算法考试中,用java是个不错的选择,它几乎实现了所有c++能实现的,所以越来越受Acmer的欢迎。总结一下用到的一些技巧和方法。更多关于csp的可参考海岛blog|皮卡丘

1. 输出

规格化的输出 System.out.printf(); // 与C中的printf用法类似. System.out.printf(“%d %10.5f\n”, a, b); // 输入b为字宽为10,右对齐,保留小数点后5位,四舍五入. // 这里0指一位数字,#指除0以外的数字(如果是0,则不显示),四舍五入.

DecimalFormat DecimalFormat fd = new DecimalFormat("#.00#"); DecimalFormat gd = new DecimalFormat("0.000"); System.out.println("x =" + fd.format(x)); System.out.println("x =" + gd.format(x));

BigInteger和BigDecimal 包括函数:add, subtract, multiply,divide, mod, compareTo等,其中加减乘除模都要求是BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转换为BigInteger(BigDecimal),用函数BigInteger.valueOf(). compareTo:根据该数值是小于、等于、或大于 val 返回 -1、0 或 1; equals:判断两数是否相等,也可以用compareTo来代替; min,max:取两个数的较小、大者;

BigInteger add(BigInteger other) BigInteger subtract(BigInteger other) BigInteger multiply(BigInteger other) BigInteger divide(BigInteger other) BigInteger mod(BigInteger other) int compareTo(BigInteger other) static BigInteger valueOf(long x)

输出大数字时直接使用 System.out.println(a) 即可。

2. 输入

读一个字符串:String s = cin.next(); 相当于 scanf(“%s”, s); 或 cin >> s;//注意是字符串而不是单个字符

3. Arrays.sort() 跟 Collection.sort()

一种是使用Comparable接口:让待排序对象所在的类实现Comparable接口,并重写Comparable接口中的compareTo()方法,缺点是只能按照一种规则排序。 (1)class Person implements Comparable{ (2)//重写该类的compareTo()方法,使其按照从小到大顺序排序

@Override public int compareTo(Person o) { return age-o.age; }

另一种方式是使用Comparator接口:编写多个排序方式类实现Comparator接口,并重写新Comparator接口中的compare()方法,在调用Arrays的sort()时将排序类对象作为参数传入:public static void sort(T[] a,Comparatorc),根据指定比较器产生的顺序对指定对象数组进行排序。数组中的所有元素都必须是通过指定比较器可相互比较的(也就是说,对于数组中的任何 e1 和 e2 元素而言,c.compare(e1, e2) 不得抛出 ClassCastException)。 //创建SortByNumber对象,将其作为参数传入

Arrays.sort(persons,sortByNumber)方法中

SortByNumber sortByNumber = new SortByNumber(); Arrays.sort(persons,sortByNumber);

//按照学号由低到高排列,创建SortByNumber类,该类实现Comparator,重写该接口的compare()

class SortByNumber implements Comparator{ //重写该接口的compare()使其按照学号由小到大排序(前者减去后者) @Override public int compare(Student o1, Student o2) { return o1.getNumber()-o2.getNumber(); } }

//按照分数由高到低排列,创建SortByScore类,该类实现Comparator,重写该接口的compare()

class SortByScore implements Comparator{ //重写该接口的compare()使其按照分数由高到低排序(后者减去前者) @Override public int compare(Student o1, Student o2) { return o2.getScore()-o1.getScore(); }

优点是可以按照多种方式排序,你要按照什么方式排序,就创建一个实现Comparator接口的排序方式类,然后将该排序类的对象传入到Arrays.sort(待排序对象,该排序方式类的对象) 我喜欢用第二种方法,而且是直接new接口(),z在方法参数中自己实现,当然如果主要的逻辑在排序这里,用第一种方法其实才是不错的选择。

4. Arrays. binarySearch()

注:此法为二分搜索法,故查询前需要用sort()方法将数组排序,如果数组没有排序,则结果是不确定的. substring()方法 是左闭右开的 ⑴ .binarySearch(object[ ], object key); 如果key在数组中,则返回搜索值的索引;否则返回-1或者”-“(插入点)。插入点是索引键将要插入数组的那一点,即第一个大于该键的元素索引。这个插入点是什么呢,刚开始我也有点困惑,不过当我看了源代码以后,就明白了,至于为什么这么做,(⊙o⊙)…,谁知道? 1.不存在时由1开始计数; 2.存在时由0开始计数。 ⑵.binarySearch(object[ ], int fromIndex, int endIndex, object key); 如果要搜索的元素key在指定的范围内,则返回搜索键的索引;否则返回-1或者”-“(插入点)。 eg: 1.该搜索键在范围内,但不在数组中,由1开始计数; 2.该搜索键在范围内,且在数组中,由0开始计数; 3.该搜索键不在范围内,且小于范围内元素,由1开始计数; 4.该搜索键不在范围内,且大于范围内元素,返回-(endIndex + 1);(特列) 对于这一点,太狠,我记不住。用的大多是找到的,找不到的返回负数来判断即可。

5. Arrays.fill()

public static void fill(Object[] a, int fromIndex, int toIndex, Object val) //将指定的 Object 引用分配



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3