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

From XGN Blog. You can also pay a visit to XGN's blog

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

笔者的成绩

目标

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

前置条件

步骤

Selling Hamburgers为例

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

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,产生如下代码:

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

比如

internal var out: PrintWriter

to

internal var out: PrintWriter? = null

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

比如

out.println("Yes")

to

out!!.println("Yes")

4.添加

fun main(){
    ClassName.main(arrayOf(""))
}

Classname是你java的类名

5.提交,AC!!

最终程序

总结

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

此外,附赠IDEA模板一份

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());
        }

    }
}

版权声明:
作者:XGN
链接:https://blog.hellholestudios.top/archives/515
来源:Hell Hole Studios Blog
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>