本人java代码本地运行正确,但提交到洛谷后报编译错误,不知原因。

P1923 【深基9.例4】求第 k 小的数

zhaohanyu0920 @ 2023-08-15 20:13:04

import java.util.ArrayList;
import java.util.Scanner;

public class Hello {
    public static void main(String[] args) {
        System.out.println("Please input the quantity of numbers: ");

        Scanner scan = new Scanner(System.in);
        int qty = scan.nextInt();
        int m=scan.nextInt();

        ArrayList<Integer> test = new ArrayList();

        for(int i = 0; i < qty; i++){
            test.add(scan.nextInt());
        }
        scan.close();
        Hello zhy = new Hello();
        zhy.quickSort(test);
        zhy.find(test,m);
    }

    public void quickSort(ArrayList<Integer> al){
        int size = al.size();
        int mid = size / 2;
        int mid_value = al.get(mid);

        ArrayList<Integer> al_left = new ArrayList();
        ArrayList<Integer> al_right = new ArrayList();

        for(int i = 0; i < size; i++){
            if(i == mid){
                continue;
            }

            int element = al.get(i);

            if(element <= mid_value){
                al_left.add(element);
            }else{
                al_right.add(element);
            }
        }

        if(al_left.size() > 1){
            quickSort(al_left);
        }

        if(al_right.size() > 1){
            quickSort(al_right);
        }

        al.clear();
        al.addAll(al_left);
        al.add(mid_value);
        al.addAll(al_right);
    }
    public void find(ArrayList<Integer> al,int index){
        int size=al.size();
        if(size<index||size<1){
            return;
        }

        int js=0;
        for(int i=0;i<al.size();i++){
            if(al.get(i)!=al.get(i+1)){
                js++;
                if(js==index){
                    System.out.println(al.get(i+1));
                    break;
                }
            }
        }
    }
}

by zhaohanyu0920 @ 2023-08-15 20:14:53

错误信息: /tmp/compiler_4pbggt35/Main.java:4: 错误: 类Hello是公共的, 应在名为 Hello.java 的文件中声明 public class Hello { ^ 1 个错误


by __DIOsama__ @ 2023-08-15 20:30:04

@zhayu__artgx 洛谷提交Java代码类名必须是Main


|