본문 바로가기

IT/알고리즘

백준 4단계(11/23/19)(10952, 10951, 1110)

반응형

1. 10952

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
public class Main {
    
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        String[] nums = null;
        do {
            nums = br.readLine().split(" ");
            if(nums[0].equals("0"&& nums[1].equals("0"))break;
            bw.write((int)(Integer.parseInt(nums[0]) + Integer.parseInt(nums[1])) + "\n");
            
        } while(true);
        
        bw.flush();
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 


2. 10951

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Scanner;
 
public class ScannerMain {
 
    public static void main(String[] args) {
        Scanner scan =  new Scanner(System.in);
        
        while(scan.hasNextInt()) {
            int firstNum = scan.nextInt();
            int secondNum = scan.nextInt();
            System.out.println(firstNum + secondNum);
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

BufferedReader를 사용한 end of file구현을 백준에선 계속 런타임에러로 인식이되서 포기... 나중에 수정예정

edit: 해결했습니다! while문에서 만약 br.readLine를 사용하면 readLine이 while문 부터 작동을해서 런타임에러로 인식을 했습니다. 그래서 readLine를 while문에서 한번적어주면 null빈칸이 올때까지 계속 실행합니다. eof

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
 
public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        String temp = " ";
        while((temp = br.readLine())!= null) {
            String[] numbers = temp.split(" ");
            try {
                int firstNum = Integer.parseInt(numbers[0]);
                int secondNum = Integer.parseInt(numbers[1]);
                int result = firstNum + secondNum;
                bw.write(result + "\n");
                bw.flush();
            } catch (Exception e) {
                continue;
            }
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 


3. 1110

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
 
public class Main {
    
    public static int result(int number) {
        
        int result = number;
        int cnt = 0;
        do {
            int a = result % 10;
            int b = result / 10;
            result = (a * 10+ (a + b) % 10;
            cnt ++;
        }while(number != result);
        
        return cnt;
    }
    
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        int number = Integer.parseInt(br.readLine());
        bw.write(String.valueOf(result(number)));
        bw.flush();
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 
반응형