在 Codeforces Java 中避免包声明 | AI生成和翻译

Home 2026.01

Question: 如果我在向 Codeforces 提交 Java 解答时,在顶部包含类似 package com.lzw.solutions.codeforces.p2190B1; 的 package 声明会发生什么?这会导致 runtime error 吗?

Answer:

是的 —— 它会导致 compilation error,而不是 runtime error。

Codeforces 不允许在提交的 Java 解答中包含 package 声明。

当你提交以下代码开头的内容时:

package com.lzw.solutions.codeforces.p2190B1;

Codeforces 编译器将拒绝它,并显示类似以下的信息:

为什么会这样

Codeforces 以一种非常简单的方式编译你的 Java 代码:

因此:

正确版本(你应该提交的内容)

只需删除 package 行

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {

    private BufferedReader in;
    private PrintWriter out;

    public Main() {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
    }

    public static void main(String[] args) throws IOException {
        Main m = new Main();
        m.solve();
        m.close();
    }

    private void close() {
        out.flush();
        out.close();
    }

    private void solve() throws IOException {
        int t = Integer.parseInt(in.readLine());
        while (t-- > 0) {
            int n = Integer.parseInt(in.readLine());
            String s = in.readLine();
            // assert (s.length() == n);   ← 在实际提交中删除或注释掉
            out.println(s);
        }
    }
}

快速总结 —— Codeforces Java 提交注意事项

祝你在 Codeforces 解题顺利!


Back

x-ai/grok-4.1-fast

Donate