我使用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);
}
}
初學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);
}
}
初學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);
}
}
真的耶,我只學到那個很長的輸入