#38176: 第一次嘗試JAVA,有點複雜-(0.3s, 1.8MB)


bobobo0413 (Andy)

學校 : 國立臺灣大學
編號 : 252359
來源 : [163.30.63.69]
最後登入時間 :
2025-08-30 10:16:01

我使用C++的想法寫JAVA,0.3s, 1.8MB

大概就是輸入的地方要注意,使用new Scanner(System.in),以及陣列取出的想法是字串分割userinput.split(" "),因為不熟JAVA,確實想超久的。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class A202310261{
    public static void main(String[] args) {
        int a=0,b=-100,c=0,d=100;
        try (Scanner scanner = new Scanner(System.in)) {
            String userinput = scanner.nextLine();
            String[] w = userinput.split(" ");
            int x = Integer.parseInt(w[0]);
            int n = Integer.parseInt(w[1]);
            String userinput2 = scanner.nextLine();
            String[] p = userinput2.split(" ");          
                       
                    for (int i = 0; i < n; i++) {
                        int e = Integer.parseInt(p[i]);
                                                   if (e > x) {
                                a++;
                               if(b<e)
                                   b=e;
                            } 
                                                   else if (e < x) {
                                                       c++;
                                                       if(d>e)
                                                           d=e;
                            
                        } 
                    }
        }
        if(a>c)
        System.out.println(a+" "+b);
        else
            System.out.println(c+" "+d);
    }
}
                    
    

#38178: Re: 第一次嘗試JAVA,有點複雜-(0.3s, 1.8MB)


liaoweichen1024@gmail.com (M_SQRT)

學校 : 新北市立新莊高級中學
編號 : 195452
來源 : [140.114.123.98]
最後登入時間 :
2025-09-24 19:10:46

初學Java的話,使用Scanner的nextInt()接收整數輸入會容易一點,程式碼也會簡潔很多。

import java.util.Scanner;

public class A202310261 {
    
    public static void main(String[] args) {
        
        int a=0, b=-100, c=0, d=100;
        
        Scanner sc = new Scanner(System.in);
        
        int x = sc.nextInt();
        int n = sc.nextInt();
        
        for (int i = 0; i < n; i++) {
            
            int e = sc.nextInt();
            
            if (e > x) {
                a++;
                if(b<e) b=e;
            } else if (e < x) {
                c++;
                if(d>e) d=e;
            } 
            
        }
        
        if(a>c) System.out.println(a+" "+b);
        else System.out.println(c+" "+d);
        
    }
    
}

#38196: Re: 第一次嘗試JAVA,有點複雜-(0.3s, 1.8MB)


bobobo0413 (Andy)

學校 : 國立臺灣大學
編號 : 252359
來源 : [163.30.63.69]
最後登入時間 :
2025-08-30 10:16:01

初學Java的話,使用Scanner的nextInt()接收整數輸入會容易一點,程式碼也會簡潔很多。

import java.util.Scanner;

public class A202310261 {
    
    public static void main(String[] args) {
        
        int a=0, b=-100, c=0, d=100;
        
        Scanner sc = new Scanner(System.in);
        
        int x = sc.nextInt();
        int n = sc.nextInt();
        
        for (int i = 0; i < n; i++) {
            
            int e = sc.nextInt();
            
            if (e > x) {
                a++;
                if(b
            } else if (e < x) {
                c++;
                if(d>e) d=e;
            } 
            
        }
        
        if(a>c) System.out.println(a+" "+b);
        else System.out.println(c+" "+d);
        
    }
    
}

真的耶,我只學到那個很長的輸入