使用Idea的Java to Kotlin功能攻略Kotlin Round

笔者在Kotlin Hero3中采用这种方法获得了top50的好成绩,为了让更多人了解到这个作弊技巧,特写此文。

笔者的成绩

目标

在不学Kotlin或基础语法不牢固的情况下在Kotlin Hero比赛中取得优异的成绩。

前置条件

步骤

Selling Hamburgers为例

1.新建一个类,写下你的Java代码:

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
import java.util.*;
import java.io.*;

/**
* A Template Used For Competitive Programming
* By XGN from HHS 2020
* Intellij is too hard to use...
* Code created at 2020/11/13 22:30
*/
public class A1431 {
public static void main(String[] args) {
A1431 me = new A1431();
me.solve(args);
}

InputReader in;
PrintWriter out;
public void solve(String[] args) {
in=new InputReader(System.in);
out=new PrintWriter(System.out);
int t=in.nextInt();
for(int i=0;i<t;i++){
subtask();
}
out.flush();
out.close();
}

void subtask(){
int n=in.nextInt();
long[] bur=new long[n];
for(int i=0;i<n;i++){
bur[i]=Long.parseLong(in.next());
}

Arrays.sort(bur);

long max=0;
for(int i=0;i<n;i++){
max=Math.max(max,bur[i]*(n-i));
}

out.println(max);
}

static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;

public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}

public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}

public int nextInt() {
return Integer.parseInt(next());
}

}
}

并且调试好

2.按Ctrl+Alt+Shift+K,产生如下代码:

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
import java.util.*
import java.io.*

/**
* A Template Used For Competitive Programming
* By XGN from HHS 2020
* Intellij is too hard to use...
* Code created at 2020/11/13 22:30
*/
class A1431 {

internal var `in`: InputReader
internal var out: PrintWriter
fun solve(args: Array<String>) {
`in` = InputReader(System.`in`)
out = PrintWriter(System.out)
val t = `in`.nextInt()
for (i in 0 until t) {
subtask()
}
out.flush()
out.close()
}

internal fun subtask() {
val n = `in`.nextInt()
val bur = LongArray(n)
for (i in 0 until n) {
bur[i] = java.lang.Long.parseLong(`in`.next())
}

Arrays.sort(bur)

var max: Long = 0
for (i in 0 until n) {
max = Math.max(max, bur[i] * (n - i))
}

out.println(max)
}

internal class InputReader(stream: InputStream) {
var reader: BufferedReader
var tokenizer: StringTokenizer? = null

init {
reader = BufferedReader(InputStreamReader(stream), 32768)
tokenizer = null
}

operator fun next(): String {
while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
try {
tokenizer = StringTokenizer(reader.readLine())
} catch (e: IOException) {
throw RuntimeException(e)
}

}
return tokenizer!!.nextToken()
}

fun nextInt(): Int {
return Integer.parseInt(next())
}

}

companion object {
@JvmStatic
fun main(args: Array<String>) {
val me = A1431()
me.solve(args)
}
}
}

3.检查编译和转换结果。一般情况下,需要在 定义在类里的非基本类型加上optional标志,即’?’并手动赋初值null

比如

1
internal var out: PrintWriter

to

1
internal var out: PrintWriter? = null

然后在要用的地方(赋值的地方不要)加上!!

比如

1
out.println("Yes")

to

1
out!!.println("Yes")

4.添加

1
2
3
fun main(){
ClassName.main(arrayOf(""))
}

Classname是你java的类名

5.提交,AC!!

最终程序

总结

此方法可以快速化不熟悉为熟悉,避免查表等问题,非常好用。

此外,附赠IDEA模板一份

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
import java.util.*;
import java.io.*;
/**
A Template Used For Competitive Programming
By XGN from HHS 2020
Intellij is too hard to use...
Code created at ${DATE} ${TIME}
*/
public class ${NAME}{
public static void main(String[] args){
${NAME} me=new ${NAME}();
me.solve(args);
}

public void solve(String[] args){
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);

//TODO code here

out.flush();
out.close();
}

static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;

public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}

public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}

public int nextInt() {
return Integer.parseInt(next());
}

}
}