๐Ÿ•ธ Algorithm/๐Ÿ•ธ ๋ฐฑ์ค€ BaekJoon

[๋ฐฑ์ค€][java][10871][X๋ณด๋‹ค ์ž‘์€ ์ˆ˜]

๋‰ด์ด NUEY 2023. 10. 15. 21:38
๋ฐ˜์‘ํ˜•

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class Main {
	// 10871	X๋ณด๋‹ค ์ž‘์€ ์ˆ˜
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		int x = sc.nextInt();
		int[] nArr = new int[n];
		
		List<String> lessThanX = new ArrayList<String>();
		
		for (int i=0; i<n; i++) {
			nArr[i] = sc.nextInt(); 
		}
		sc.close();
		
		for (int a : nArr) {
			if (a < x)
				lessThanX.add(a + " ");
		} 
		
		Iterator<String> lessThanXiter = lessThanX.iterator();
		
		while (lessThanXiter.hasNext())
			System.out.print(lessThanXiter.next());
	}
}
  • ๊ธฐ๋ณธ ๋ฐฐ์—ด์€ ํฌ๊ธฐ๋ฅผ ์ •ํ•˜๊ณ  ํ•ด์•ผ ํ•ด์„œ, ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ ธ์žˆ์„ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ–ˆ๊ตฌ์š”
  • ์ถœ๋ ฅํ•  ๊ฐ’์„ ๋‹ด์„ ๊ฒฝ์šฐ์—” ArrayList๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
  • list๋ฅผ ์ถœ๋ ฅํ•  ๋• Iterator๋กœ ๋‹จ์ˆœํ•˜๊ฒŒ ์ถœ๋ ฅํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

 

 

๋ฐ˜์‘ํ˜•