java for problem solving - GitDeveloperKim/DreamEach GitHub Wiki
- Ctrl + Shift + O : ์๋ import
- ๊ฐ๋จํ ์ ์ถ๋ ฅ (ํ ์คํธ ์ผ์ด์ค ์์ด ์ ๋ ฅ์ด ์ ์ ๊ฒฝ์ฐ์ ์ฌ์ฉ)
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int a = 100;
double d = 1.1234567;
String str = String.format("๋ฌธ์์ด ์์: %05d", a); // string ์ผ๋ก ๋ง๋ค๊ธฐ
System.out.printf("%5d \n", a); // ์๋ ๊ฐํ ์๋จ
System.out.printf("%.3f\n", d); // %.nf ์์์ ์๋ n๋ฒ์งธ ์๋ฆฌ๊น์ง ์ถ๋ ฅ (๋ฐ์ฌ๋ฆผ)
- import ์ถ๊ฐ
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
- throws ์ถ๊ฐ
import java.io.IOException;
public static void main(String[] args) throws IOException {}
- try with resources ์ฌ์ฉ
example
// try with resources
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
} catch (IOException e) {
}
- ํ์ผ๋ก๋ถํฐ input ๋ฐ๋ ๋ฒ
public static void main(String[] args) throws FileNotFoundException {
System.setIn(new FileInputStream("C:\\filePath.txt"));
}- BufferedReader ์ฌ์ฉ๋ฒ
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //์ ์ธ
String s = br.readLine(); //String
int i = Integer.parseInt(br.readLine()); //Int
String [] str = br.readLine().split(" "); // ๋์ด์ฐ๊ธฐ๋ก ์คํธ๋ง ์ ์ฅ
- StringTokenizer ์ฌ์ฉ๋ฒ
StringTokenizer๋ line๋จ์๋ก ๊ฐฑ์ ํด์ค์ผํจ
StringTokenizer st = new StringTokenizer(br.readLine()); //StringTokenizer์ธ์๊ฐ์ ์
๋ ฅ ๋ฌธ์์ด ๋ฃ์
int a = Integer.parseInt(st.nextToken()); //์ฒซ๋ฒ์งธ ํธ์ถ
int b = Integer.parseInt(st.nextToken()); //๋๋ฒ์งธ ํธ์ถ
String array[] = s.split(" "); //๊ณต๋ฐฑ๋ง๋ค ๋ฐ์ดํฐ ๋์ด์ ๋ฐฐ์ด์ ๋ฃ์
-
String vs StringBuilder vs StringBuffer
String ๋น๊ฐ๋ณ ์คํธ๋ง
StringBuilder ๊ฐ๋ณ ์คํธ๋ง, ๋น ๋ฆ
StringBuffer ๊ฐ๋ณ ์คํธ๋ง, ๋๊ธฐํ๊ฐ ํ์ํ ์ฐ์ฐ์ ์ฌ์ฉ, ๋๋ฆผ
click -
BufferedWriter ์ฌ์ฉ๋ฒ
BufferedWriter์ ๊ฒฝ์ฐ ๋ฒํผ๋ฅผ ์ก์๋์๊ธฐ ๋๋ฌธ์ ๋ฐ๋์ flush() / close () ๋ฅผ ๋ฐ๋์ ํธ์ถํด ์ฃผ์ด ๋ค์ฒ๋ฆฌ๋ฅผ ํด์ฃผ์ด์ผ ํ๋ค.
์๋ ๊ฐํ ๊ธฐ๋ฅ์ด ์๊ธฐ ๋๋ฌธ์ ๊ฐํ์ ํด์ฃผ์ด์ผ ํ ๊ฒฝ์ฐ์๋ \n๋ฅผ ํตํด ๋ฐ๋ก ์ฒ๋ฆฌํด ์ฃผ์ด์ผ ํ๋ค.
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); //ํ ๋น๋ ๋ฒํผ์ ๊ฐ ๋ฃ์ด์ฃผ๊ธฐ
StringBuilder s = "abcdefg"; // ์ถ๋ ฅํ ๋ฌธ์์ด
s.append(answer+""); // ์ ๋ต ๋ฌธ์์ด ์ถ๊ฐ
bw.write(s.toString()); // ๋ฒํผ์ ์๋ ๊ฐ ์ ๋ถ ์ถ๋ ฅ
bw.flush(); //๋จ์์๋ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ ์ถ๋ ฅ์ํด
bw.close(); //์คํธ๋ฆผ์ ๋ซ์
- ์ถ์ฒ click
011
111
110
for (int i = 1; i <= N; i++) {
String temp = br.readLine();
for (int j = 1; j <= M; j++) {
input[i][j] = temp.charAt(j-1)-'0'; // ์ซ์๋ก ๋ณ๊ฒฝ ์ฝ๋
}
}
- ์ ๋ ฅ ๊ฐฏ์๊ฐ ์ฃผ์ด์ง์ง ์์์ ๋
- ์ฐธ๊ณ
- ๋ฐฑ์ค 5639
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
// String str = null;
String str = ""; // ์ด๊ธฐํ ์ํด์ฃผ๋ฉด ๋ฐฑ์ค์์ nullpointer exception ๋ฐ์ ์ํด
while ((str=br.readLine()) != null && str.length() != 0) {
StringTokenizer st = new StringTokenizer (str);
....
}
BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); // ์ ๋ ฌํ์ฌ ๋ฐฐ์ด์ ๋ฃ๋๋ฒ Stream int [] arr = Arrays.stream(br.readLine().split(" ")) .mapToInt(Integer::parseInt) .sorted() .toArray();// .... // answer ๋ ๋ฆฌ์คํธ, ArrayListํ // ์ ๋ ฌํ์ฌ ์ถ๋ ฅํ๊ธฐ answer.stream() .sorted() .forEach(d->System.out.print(d+" ")); //.forEach(System.out::println); //.forEach(System.out::print);
- Integer.parseInt(string);
- valueOf(Integer);
- int ํ ๋ฒ์ -21์ต ~ 21์ต
- double
Arrays.sort(arr, (int []a, int []b)->a[0] - b[0]); // ์ค๋ฆ์ฐจ์
- Comparable - ์ด ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ฐ์ฒด ์ค์ค๋ก์๊ฒ ๋ถ์ฌํ๋ ํ ๊ฐ์ง ๊ธฐ๋ณธ ์ ๋ ฌ ๊ท์น์ ์ค์ ํ๋ ๋ชฉ์ ์ผ๋ก ์ฌ์ฉ
- Comparator - ์ด ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์คํ ์ ๋ ฌ ๊ท์น ๊ทธ ์์ฒด๋ฅผ ์๋ฏธํ๋ฉฐ, ๊ธฐ๋ณธ ์ ๋ ฌ ๊ท์น๊ณผ ๋ค๋ฅด๊ฒ ์ํ๋ ๋๋ก ์ ๋ ฌ ์์๋ฅผ ์ง์
class Node implements Comparable {
int dst; // ๋ค์ต์คํธ๋ผ-> next node
int val; // ๋ค์ต์คํธ๋ผ-> ๊ฐ์ ์ ๊ฐ์ค์น
// Constructor
Node (int dst, int val) {
this.dst = dst;
this.val = val;
}
@Override
public int compareTo(Node next) {
return this.val - next.val; // ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
}
}
class Point implements Comparable {
int node;
int value;
Point (int node, int value) {
this.node = node;
this.value = value;
}
@Override
public int compareTo(Point o) {
// ์ค๋ฆ์ฐจ์
if (this.value - o.value > 0) {
return 1;
} else if (this.value - o.value == 0) {
return 0;
} else {
return -1;
}
}
}
- ๋ฎ์ ์ซ์๋ถํฐ ํฐ์ซ์๋ก ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ
- Collections.serverOrder(), Comparable() ๋ฅผ ์ด์ฉํ์ฌ ๋ณ๊ฒฝ
- peek() : top์ด ๊ฐ๋ฆฌํค๋ ๋ฐ์ดํฐ, ์ฐ์ ์์๊ฐ ๊ฐ์ฅ ๋์ ๋ฐ์ดํฐ
- poll() : ๊ฐ์ ๋ฐํํ์ฌ ์ญ์
- offer(), add() : ๊ฐ์ ์ ๋ ฅํ๋ค
package Practice;
import java.util.PriorityQueue;
public class Practice_20191110 {
public static void main(String[] args) {
PriorityQueue heap = new PriorityQueue<>(new Comparator() {
@Override
public int compare(int [] o1, int[] o2) {
return o1[1] - o2[1]; // ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
}
});
heap.add(new int[] {100,5});
heap.add(new int[] {99,4});
heap.add(new int[] {98,3});
heap.add(new int[] {97,2});
heap.add(new int[] {96,1});
System.out.println("heap.poll()="+heap.poll()[0]); // 2๋ฒ์งธ ์ธ์๋ก ๋น๊ต, 96์ถ๋ ฅ
}
}
์ถ์ฒ click
pq = new PriorityQueue<>((int [] a, int [] b)-> (a[1] > b[1])? 1: -1); // int๋ฐฐ์ด ๋๋ฒ์งธ ์์๋ฅผ ์ด์ฉํ์ฌ ์ค๋ฆ์ฐจ์์ผ๋ก ์ฐ์ ์์ ํ ์ ์ธ