Algorithm/Problem Solving

[BackJoon] 9517문제 아이러브크로아티아

Jinlib 2017. 5. 2. 01:36

문제 URL https://www.acmicpc.net/problem/9517


1.문제의 접근을 하기전에 알아야 할 정보가 3가지.

1) 입력을 받을때 next메서드과 nextInt메서드의 차이.

자세히 모른다면 참조

2) 문자열을 분리하는법.

자세히 모른다면 참조

3) String형을 int형으로 형변환

자세히 모른다면 참조


2. 내 코드

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
package i_love_croatia;
import java.util.Scanner;
public class main {
    public static void main(String[] args){
        int time_limit = 210;
        Scanner scan = new Scanner(System.in);
        int boom = scan.nextInt();                        //폭탄을 가지고 있는 사람
        int question = scan.nextInt();                     //문제 수 만큼 반복
        scan.nextLine();                                        //개행문자를 제거하기 위해 사용
        for(int i = 0; i<question;i++){
            System.out.print("input ");
            String str =scan.nextLine();                        //문제에 걸린시간과 T N P 입력
            int time = Integer.parseInt(str.split(" ")[0]);     //입력받은 값을 int형으로 변경해서 time에 저장
            String answer = str.split(" ")[1];                //대답을 answer에 저장
            time_limit = time_limit - time;                    //입력된 time을 계속하여 뺌
            if(time_limit>0){                                
                if( answer.equals("T"))
                    boom++;                            //대답이 T일경우에만 폭탄을 넘긴다
            }
            if(boom==9)                                //boom이 8을 넘어가면 1부터 다시한다.
                boom=1;
        }
        System.out.println(boom);    
    }
}
 
cs

코드에 대한 설명은 주석에 달아 놓았으므로 생략한다.


3. 본 받을 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.io.*;
import java.util.*;
class Main{
    public static void main(String[] args)throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer s;
        int who=Integer.parseInt(br.readLine().trim())-1;
        int time=0;
        int N=Integer.parseInt(br.readLine().trim());
        for(int i=0;i<N;i++){
            s=new StringTokenizer(br.readLine().trim());
            time+=Integer.parseInt(s.nextToken());
            if(time>210){
                System.out.print(who+1);
                break;
            }
            if(s.nextToken().charAt(0)=='T'){
                who=(who+1)%8;
            }
        }
 
    }
}
 
cs

1) 배울

내가 쓴 코드보다 메모리 효율성이 좋고 새로운 클래스를 사용했다.

  1. Scanner 클래스를 사용하지 않고 BufferedReader