posted by 귀염둥이채원 2019. 3. 3. 21:58

# 자바(java) 프로그램 코드의 시간 측정 방법

https://palyoung.tistory.com/41


long beforeTime = System.currentTimeMillis();

****** 소스 코드 ******

System.out.println("count= " + count);

long afterTime = System.currentTimeMillis();

long secDiffTime = (afterTime - beforeTime);

System.out.println("시간차이(msec) : " + secDiffTime);


# 시간복잡도(Time Complexity), 빅오(Big-O)표기법이란?

https://palyoung.tistory.com/37


# String to int, int to String 변환

https://palyoung.tistory.com/52


# String값을 int형의 값으로 바꾸는 방법

int numInt = Integer.parseInt(numStr);


# int형의 값을 String으로 바꾸는 방법

String numStr2 = String.valueOf(numInt);


# ArrayList 사용법

https://palyoung.tistory.com/50


# StringTokenizer 사용법 정리

https://palyoung.tistory.com/54


# String vs StringBuffer vs StringBuiler

https://palyoung.tistory.com/53

StringBuilder가 성능이 좋다.


# Scanner vs BufferedReader 차이

https://palyoung.tistory.com/55

BufferedReader가 성능이 좋다.



'PS(Problem Solving) > 공통' 카테고리의 다른 글

백준 사이트 정리  (0) 2019.03.01
알고리즘 소스 템플릿  (0) 2019.02.27
알고리즘 문제 풀이 템플릿  (0) 2019.02.27
알고리즘 학습 관련 자료 모음  (0) 2019.02.27
posted by 귀염둥이채원 2019. 3. 3. 21:48

# Scanner

- Scanner는 사용하기 편리지만 속도가 느리다.

- 공란과 줄바꿈을 입력값의 경계로 사용하며, 입력받은 즉시 자료형이 확정된다.


# BufferedReader

- 일반적으로 라인단위로 입력을 받는다.

- 입력받은 값이 String 타입이므로 타입변환이 필요해서 불편함이 존재

- throws Exception 혹은 try ~ catch 를 이용해서 Exception을 따로 처리가 필요


하지만 많은 양의 데이터를 입력받을경우 BufferedReader를 통해 입력받는 것이 효율면에서 훨씬 낫습니다.알고리즘 문제를 풀때는 BufferedReader를 사용하는 것이 좋다.


# 성능 차이 참고

https://algospot.com/forum/read/2496/


# Scanner 예제 소스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;
 
public class ScannerPractice {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
 
        // Integer Type Input
        int intValue = sc.nextInt();
 
        // Long Type Input
        long longValue = sc.nextLong();
        
        // Double Type Input
        double doubleValue = sc.nextDouble();
 
        // String Type Input
        String strValue = sc.next();
 
        // Boolean Type Input
        boolean boolValue = sc.nextBoolean();
    }
}
 
cs


# BufferedReader 예제 소스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class BufferedReaderTest {
    // BufferedReader는 Exception이 처리를 따로 해줘야 하기 때문에 throws를 해주거나 
    // try ~ catch로 예외처리를 해줘야합니다.
    public static void main(String[] args) throws Exception {
        // BufferedReader 객체 생성
        // new InputStreamReader 및 System.in
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        // StringTokenizer 객체 선언
        StringTokenizer st = null;
 
        // String Line이므로 Integer.parseInt를 이용하여 형변환해야함
        int n = Integer.parseInt(br.readLine());
 
        // "1 3 5 7" 식으로 공란 포함 String Line일시 StringTokenizer 이용
        int[] arrays = new int[n + 1];
        st = new StringTokenizer(br.readLine());
        for (int i = 1; i <= n; i++) {
            // 배열에다 토큰을 하나씩 불러서 입력해줌
            arrays[i] = Integer.parseInt(st.nextToken());
        }
    }
}
 
cs


# BufferedReader + StringBuiler 예제 소스

알고리즘 풀때는 한꺼번에 모아서 입력을 받고 한꺼번에 모아서 출력하는 것이 성능이 좋다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
    public static void main(String[] args) throws IOException{
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
 
        int N = Integer.parseInt(br.readLine());
        int[] arr = new int[10010];
 
        for (int i = 0; i< N;i++){
            arr[Integer.parseInt(br.readLine())]++;
        }
 
        for(int i =0; i<= 10000; i++){
            while(arr[i] != 0){
                sb.append(""+i+"\n");
                arr[i]--;
            }
            
        }
        System.out.println(sb.toString());
    }
}
 
 
출처: https://lifeignite.tistory.com/34 [게임 && PS ignite]
cs


# 참고사이트

https://cocomo.tistory.com/507

https://coding-factory.tistory.com/251

https://mygumi.tistory.com/236

https://code0xff.tistory.com/5

https://lifeignite.tistory.com/34

posted by 귀염둥이채원 2019. 3. 3. 21:18

자바는 String을 token단위로 끊어주는 StringTokenizer 클래스를 제공한다.


# 생성자

1. StringTokenizer(String str)

- str : 분석할 문자열

- 기본 분리 문자를 사용합니다. 

  기본 분리 문자에는 공백문자, 탭문자, 개행문자, 캐리지리턴문자가 있습니다.


2. StringTokenizer(String str , String delim)

- str : 분석할 문자열

- delim : 분리 문자로 사용할 문자


3. StringTokenizer(String str , String delim , boolean returnDelims)

- str : 분석할 문자열

- delim : 분리 문자로 사용할 문자

- returnDelims : 분리 문자까지 분리해서 출력할지 여부


# 주요함수

countTokens(): 토큰의 갯수를 리턴한다.

nextToken(): 다음 토큰을 리턴한다. 이전 토큰은 제거한다.

nextToken(String delim): 구획문자(delimiter)를 delim으로 바꾼 후 바뀐 다음 토큰을 리턴한다.

hasMoreTokens(): 리턴할 다음 토큰이 있으면 true를 다음 토큰이 없으면 false를 리턴한다.


# 예제 소스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Main {
    public static void main(String[] args) {
            String str = "this is my string"
            StringTokenizer st = new StringTokenizer(str); 
            System.out.println(st.countTokens()); 
            
            while(st.hasMoreTokens()) { 
                System.out.println(st.nextToken()); 
            }
            System.out.println(st.countTokens());
    }
}
 
/*********** 결과 ***************
4
this
is
my
string
0
*/
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.StringTokenizer;
 
public class Main {
    public static void main(String[] args) {
        String str = "this%^is%^my%^string"
        
        StringTokenizer st = new StringTokenizer(str,"%^");
        System.out.println(st.nextToken()); 
        System.out.println(st.nextToken());
        System.out.println(st.nextToken());
        System.out.println(st.nextToken());
    }
}
 
/*********** 결과 ***************
this
is
my
string
*/
cs


# 참고사이트

https://arer.tistory.com/48

https://dlfma1985.tistory.com/52