Java相关的竞赛模板

因为某个der比赛只支持Java和JS参赛,临时速成Java,这里是赶鸭子上架的ACMJava模板。

东大模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import static java.lang.Math.*;
import static java.util.Arrays.*;
import java.io.*;
import java.util.*;

public class Main {
static boolean LOCAL = System.getSecurityManager() == null;
static boolean TO_FILE = true;
Scanner sc = new Scanner(System.in);

void run() {

}

void debug(Object... os) {
System.err.println(deepToString(os));
}

public static void main(String[] args) {
if (LOCAL) {
try {
System.setIn(new FileInputStream("./bin/in.txt"));
} catch (Throwable e) {
LOCAL = false;
}
}
if (TO_FILE) {
try {
System.setOut(new PrintStream("./src/output.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
new Main().run();
}
}

Kattio

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// 标准 IO
public Kattio() { this(System.in, System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// 文件 IO
public Kattio(String intput, String output) throws IOException {
super(output);
r = new BufferedReader(new FileReader(intput));
}
// 在没有其他输入时返回 null
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) {}
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}

杂项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//import java.util.Scanner;//相当于输入输出头文件
import java.util.*;//啥都有头文件
//import java.math.BigInteger;//大数类
import java.math.BigDecimal;//大浮点数
import java.math.*;//关于数学的头文件
public class Main {
public static void main(String[] args) {
BigInteger[] sum=new BigInteger[105];//开一个数组
Scanner cin = new Scanner(System.in);//必须要写这句话(输入)
int t = cin.nextInt();//输入整数T nextInt() 下一个int型数据
// while(cin.hasNext()) {//循环输入,hasNext()
// int n = cin.nextInt();
// }
while (t--!=0) {//java里不能直接写while(t--),必须是只有真假的表达式
BigInteger a = cin.nextBigInteger();//输入下一个大数
BigInteger b = cin.nextBigInteger();//输入下一个大数
BigInteger c;
c = a.add(b);//c=a+b
c = a.subtract(b);//c=a-b
c = a.multiply(b);//c=a*b
c = a.divide(b);//c=a/b(整数除法)
c = a.remainder(b);//c=a%b
c = a.mod(b);//c=a%b(结果为正数)
c = BigInteger.ZERO;//c=0
c = BigInteger.ONE;//c=1
c = BigInteger.TEN;//c=10 java大数中只有0,1,10
c = BigInteger.valueOf(7);//c=7
int n = 7;
c = BigInteger.valueOf(n);//c=n
c = a.pow(n);//幂运算 c=pow(a,n)
c = a.gcd(b);//最大公约数 c=gcd(a,b)
c = a.abs();//
c = a.max(b);//c=max(a,b)
c = a.min(b);//c=min(a,b)
n = a.compareTo(b);//a<b n=-1;a=b n=0;a>b n=1
a.equals(b);//a==b
a.isProbablePrime(2);//判断素数,有一定几率错误,最好手写
System.out.println(c);//输出c并换行
System.out.print(c);//输出c
System.out.println(a+" "+b);//输出a空格b
BigDecimal d = cin.nextBigDecimal();//输入下一个高精度浮点数
BigDecimal e = cin.nextBigDecimal();//输入下一个高精度浮点数
BigDecimal f = d.add(e);//f=d+e
//...其他和大数类似
f = d.setScale(2);//保留2位小数,默认四舍五入
f = d.setScale(2,BigDecimal.ROUND_DOWN);//保留2位小数,向下取整
f = d.setScale(2,BigDecimal.ROUND_UP);//保留2位小数,向上取整
f = d.setScale(2,BigDecimal.ROUND_HALF_DOWN);//保留2位小数,四舍五入,正好.5舍去
f = d.setScale(2,BigDecimal.ROUND_HALF_UP);//保留2位小数,四舍五入,正好.5进位

Dog[]dogs = new Dog[] {d1,d2,d3,d4,d5};
Arrays.sort(dogs,new ByWeightComparator());
}
}
}

class Dog //定义一个“狗类”
{
private String name;
private int weight;
public Dog(String name, int weight)
{
this.setName(name);
this.weight = weight;
}
public int getWeight()
{
return weight;
}
public void setWeight(int weight)
{this.weight = weight;}
public void setName(Stringname)
{this.name = name;}
public String getName()
{return name;}
}
class ByWeightComparator implements Comparator
{
public final int compare(Object pFirst, Object pSecond)
{
int aFirstWeight = ((Dog)pFirst).getWeight();
int aSecondWeight = ((Dog)pSecond).getWeight();
int diff = aFirstWeight - aSecondWeight;
if(diff >= 0)
return -1;
else if(diff < 0)
return 1;
return 0;
}
}

类STL

优先队列PriorityQueue

类及其迭代器实现Collection和Iterator接口的所有可选方法,PriorityQueue是非线程安全的,所以Java提供了PriorityBlockingQueue(实现BlockingQueue接口)用于Java多线程环境。默认是小根堆。PriorityQueue的iterator()不保证以任何特定顺序遍历队列元素。若想按特定顺序遍历,先将队列转成数组,然后排序遍历。

1
PriorityQueue<Integer> queue = new PriorityQueue<>();

大根堆

1
2
3
4
5
6
7
8
PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2)->o2.compareTo(o1));
PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2)->o2.compareTo(o1));

操作

1
2
3
4
5
6
7
8
9
10
11
12
13
queue.add(E e);// 插入元素e
queue.clear();// 清空
queue.offer(12);// 插入
queue.offer(15);
queue.offer(10);
queue.peek(); // 检索但不删除此队列的头,如果此队列为空,则返回 null
queue.poll(); // 检索并删除此队列的头,如果此队列为空,则返回 null
queue.size(); // 返回此集合中的元素数
queue.toArray(); // 返回一个包含此队列中所有元素的数组
while(!queue.isEmpty()) {
int t = queue.poll();
System.out.println(t);
}