티스토리 뷰

https://www.acmicpc.net/problem/15552

 

15552번: 빠른 A+B

첫 줄에 테스트케이스의 개수 T가 주어진다. T는 최대 1,000,000이다. 다음 T줄에는 각각 두 정수 A와 B가 주어진다. A와 B는 1 이상, 1,000 이하이다.

www.acmicpc.net

 

BufferedReader/ BufferedWriter는 우리가 통상적으로 사용하는 Scanner 클래스보다 가동속도가 빠르다고 하는데 개론적인 부분은 나중에 추가적으로 공부하기로,,

 

제출코드

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package practice;
 
 
 
public class Main {
    public static void main(String args[]){
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
            
            int i, t, a, b = 0;
            String s = "";
        
            t = Integer.parseInt(br.readLine());
 
            for( i = 0; i < t ; i++ ){
                s = br.readLine();
                String arr[] = s.split(" ");
                a = Integer.parseInt(arr[0]);
                b = Integer.parseInt(arr[1]);
                
                bw.write( a + b + "\n");
            }
            bw.flush();
            bw.close();
        }catch(IOException e){
            
        }
        
        
    }
}
 

 

풀이 및 삽질

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package practice;
 
 
public class Main {
    //입력하는건 BufferedReader
    //출력하는 건 BufferedWriter를 통해서 출력함
    public static void main(String args[]){
        //buffered를 사용하려면 예외처리를 해줘야하는데 throws Exception의 경우 매번 try-catch를 받아 사용하기가
부담스러워서 사용하는것이기때문에 여기서는 바로 try-catch를 사용함
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
            
            int i, t, a, b = 0;
            String s = "";
        
            t = Integer.parseInt(br.readLine());
            
            //하나씩 집어넣는 방법
//            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
//            a = Integer.parseInt(st.nextToken());
//            b = Integer.parseInt(st.nextToken()); -> for,while문에서는 next()함수를 사용할 수 없어서 split으로 받아줌
 
            for( i = 0; i < t ; i++ ){
                s = br.readLine();
                String arr[] = s.split(" ");
                a = Integer.parseInt(arr[0]);
                b = Integer.parseInt(arr[1]);
                
                bw.write( a + b + "\n");
            }
            bw.flush();
            bw.close();
        }catch(IOException e){
            
        }
        
        
    }
}
 
 
 
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
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
글 보관함