10871번 - X보다 작은 수
<맞는코드>
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int x = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; ++i) { a[i] = sc.nextInt(); } sc.close(); for (int i = 0; i < a.length; ++i) { if (a[i] < x) { System.out.print(a[i] + " "); } } }}
<틀린코드>
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; ++i) { a[i] = sc.nextInt(); } sc.close(); for (int i = 0; i < a.length; ++i) { if (a[i] < x) { System.out.print(a[i] + " "); } } }}
->이렇게 요소 생성 순서만 다르게 했는데 틀렸다고 나와서요... 이유가 뭘까요?
sc.nextInt()가 무슨 함수인지 이해하신 뒤에 다시 생각해보세요.
댓글을 작성하려면 로그인해야 합니다.
parkjji23 3년 전
<맞는코드>
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int x = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < a.length; ++i) {
a[i] = sc.nextInt();
}
sc.close();
for (int i = 0; i < a.length; ++i) {
if (a[i] < x) {
System.out.print(a[i] + " ");
}
}
}
}
<틀린코드>
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < a.length; ++i) {
a[i] = sc.nextInt();
}
sc.close();
for (int i = 0; i < a.length; ++i) {
if (a[i] < x) {
System.out.print(a[i] + " ");
}
}
}
}
->이렇게 요소 생성 순서만 다르게 했는데 틀렸다고 나와서요... 이유가 뭘까요?