Post

baekjoon 3단계 반복문

기록용 baekjoon 단계별 풀이입니다. 언어 : Java

2739번


1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.Scanner;  
public class Main {  
    public static void main(String[] args) {  
        Scanner sc = new Scanner(System.in);  
  
        int n = sc.nextInt();  
  
        for(int i=1;i<=9;i++)  
        {  
            System.out.println(n+" * "+i +" = "+n*i);  
        }  
    }  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import javax.swing.*;  
import java.util.*;  
import java.io.*;  
public class Main {  
    public static void main(String[] args) throws IOException {  
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));  
  
        int n = Integer.parseInt(bf.readLine());  
  
        for(int i=1;i<=9;i++)  
        {  
            System.out.println(n+" * "+i+" = "+ n*i);  
        }  
    }  
}

10950번


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Scanner;  
public class Main {  
    public static void main(String[] args)  {  
       Scanner sc = new Scanner(System.in);  
  
       int T = sc.nextInt();  
       int arr[]= new int[T];  
  
       for(int i=0;i<T;i++)  
       {  
           int a = sc.nextInt();  
           int b = sc.nextInt();  
           arr[i]=a+b;  
       }  
       for(int result : arr){  
           System.out.println(result);  
       }  
    }  
}

8393번


1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Scanner;  
public class Main {  
    public static void main(String[] args) {  
        Scanner sc = new Scanner(System.in);  
  
        int num = sc.nextInt();  
        int sum = 0;  
        for(int i=1;i<=num;i++)  
        {  
            sum+=i;  
        }  
        System.out.println(sum);  
    }  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.*;  
import java.io.*;  
public class Main {  
    public static void main(String[] args) throws IOException{  
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));  
        int num = Integer.parseInt(bf.readLine());  
        int sum =0;  
  
        for(int i=1;i<=num;i++) sum+=i;  
  
        System.out.println(sum);  
    }  
}

25304번


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 Main {  
    public static void main(String[] args) {  
        Scanner sc = new Scanner(System.in);  
  
        int X = sc.nextInt();  
        int N = sc.nextInt();  
        int result = 0;  
  
        for(int i=0;i<N;i++)  
        {  
            int a = sc.nextInt();  
            int b = sc.nextInt();  
            result +=a*b;  
        }  
        if(result == X){  
            System.out.println("Yes");  
        }  
        else{  
            System.out.println("No");  
        }  
    }  
}
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.util.*;  
import java.io.*;  
public class Main {  
    public static void main(String[] args) throws IOException {  
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); //키보드 입력 바이트 스트림을 char 타입으로 처리 후, string 타입으로 변환 후 저장  
  
        int X = Integer.parseInt(bf.readLine());  
        int n = Integer.parseInt(bf.readLine());  
        int a,b;  
        int sum = 0;  
        StringTokenizer st;  
        for(int i=0;i<n;i++)  
        {  
            st = new StringTokenizer(bf.readLine()," ");  
            a = Integer.parseInt(st.nextToken());  
            b = Integer.parseInt(st.nextToken());  
            sum += a*b;  
        }  
        if(X == sum)  
        {  
            System.out.println("Yes");  
        }  
        else{  
            System.out.println("No");  
        }  
  
    }  
}

25314번


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.Scanner;  
public class Main {  
    public static void main(String[] args) {  
        Scanner sc = new Scanner(System.in);  
  
        int N = sc.nextInt();  
        String str ="";  
  
        for(int i=1;i<=N/4;i++)  
        {  
            str += "long ";  
        }  
        System.out.println(str+"int");  
    }  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
import java.io.*;  
public class Main {  
    public static void main(String[] args) throws IOException {  
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); 
        int n = Integer.parseInt(bf.readLine());  
        String str ="long ";  
        for(int i=0;i<(n/4);i++)  
        {  
            System.out.print(str);  
        }  
        System.out.print("int");  
    }  
}

15552번


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int T = Integer.parseInt(bf.readLine());
        StringTokenizer st;

        for(int i=0;i<T;i++)
        {
            st = new StringTokenizer(bf.readLine()," ");
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());

            bw.write(a+b+"\n");
        }
        bw.flush();
        bw.close();
    }
}
  • bufferedReader는 입력 받은 모든 데이터가 String으로 반한됨.

  • 숫자 형식을 받기 위해서 형변환이 필요함. Integer.parseInt

  • 메소드 선언부에 throws를 명시하면 해당 메소드 내에서 exception이 발생하는 경우 해당 메소드를 호출한 곳에서 예외가 발생함.

  • readLint() 시마다 예외처리가 반드시 필요하기 때문에 throws IOEcxeption 사용

  • BufferedReader의 경우 close()를 콜해주지 않아도 문제는 없지만 BufferWriter의 경우 close()를 해주지 않으면 정상적으로 진행되지 않을 수 있음

  • 11021번


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.sql.SQLOutput;  
import java.util.*;  
import java.io.*;  
public class Main {  
    public static void main(String[] args) throws IOException {  
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));  
  
        int T = Integer.parseInt(bf.readLine());  
  
        StringTokenizer st;  
  
        for(int i=1;i<=T;i++)  
        {  
            st = new StringTokenizer(bf.readLine(), " ");  
            int A = Integer.parseInt(st.nextToken());  
            int B = Integer.parseInt(st.nextToken());  
  
            System.out.println("Case #"+i+": "+(A+B));  
        }  
        bf.close();  
    }  
}

11022번


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.*;  
import java.io.*;  
public class Main {  
    public static void main(String[] args) throws IOException {  
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));  
  
        int T = Integer.parseInt(bf.readLine());  
  
        StringTokenizer st;  
  
        for(int i=1;i<=T;i++)  
        {  
            st = new StringTokenizer(bf.readLine()," ");  
            int A = Integer.parseInt(st.nextToken());  
            int B = Integer.parseInt(st.nextToken());  
  
            System.out.println("Case #"+i+": "+A+" + "+B+" = "+(A+B));  
        }  
        bf.close();  
    }  
}

2438번


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.*;  
import java.io.*;  
public class Main {  
    public static void main(String[] args) throws IOException {  
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));  
  
        int N = Integer.parseInt(bf.readLine());  
        String st = "";  
  
        for(int i=1;i<=N;i++)  
        {  
            for(int j=1;j<=i;j++)  
            {  
                System.out.print("*");  
            }  
            System.out.print("\n");  
        }  
    }  
}

2439번 - 다시


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;  
public class Main {  
    public static void main(String[] args){  
        Scanner sc = new Scanner(System.in);  
  
        int num = sc.nextInt();  
  
        for(int i=1;i<=num;i++)  
        {  
            for(int j=1;j<=num-i;j++)  
            {  
                System.out.print(" ");  
            }  
            for(int k=0;k<i;k++)  
            {  
                System.out.print("*");  
            }  
            System.out.println();  
        }  
    }  
}

10952번


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.*;  
import java.io.*;  
public class Main {  
    public static void main(String[] args) throws IOException {  
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));  
  
        StringTokenizer st;  
  
        while (true)  
        {  
            st = new StringTokenizer(bf.readLine());  
            int A = Integer.parseInt(st.nextToken());  
            int B = Integer.parseInt(st.nextToken());  
  
            if(A==0 && B==0)  
            {  
                break;  
            }  
            System.out.println(A+B);  
        }  
        bf.close();  
    }  
}

10951번 - 다시


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;  
import java.io.*;  
public class Main {  
    public static void main(String[] args) throws IOException {  
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));  //선언
  
        StringTokenizer st;  //문자열 분리
        String str;  
        while ((str=bf.readLine())!=null)  //str에 저장된 데이터가 null일경우 종료
        {  
            st = new StringTokenizer(str," ");  //공백 단위로 데이터를 가공
            int A = Integer.parseInt(st.nextToken()); //토큰을 하나씩 꺼내옴
            int B = Integer.parseInt(st.nextToken());  
  
            System.out.println(A+B);  
        }  
    }  
}
  • Eof(End of File) : 문제에 입력이 끝나는 지점, 조건이 없음. 파일의 긑에서 입력 받을 데이터가 없으면 알아서 종료 시킴.

  • StringTokenizer 클래스 : 문자열이 한 종류의 구분자로 연결되어 있을 경우 문자열(토큰)을 분리해내기 위한 클래스

This post is licensed under CC BY 4.0 by the author.