IT's 우

[java] baekjoon 백준 단계별로 풀기- 3단계 반복문 본문

알고리즘/백준

[java] baekjoon 백준 단계별로 풀기- 3단계 반복문

디우 2022. 4. 26. 23:46
728x90

 

2739번- 구구단
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.Scanner;
 
public class n2739 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
 
        int a = s.nextInt(); // N을 입력 받음
 
        for (int i = 1; i <= 9; i++) {
            System.out.println(a + " * " + i + " = " + a * i);
        }
 
    }
}
 
cs

 

10950번- A+B - 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;
 
public class N10950 {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        
        int x=s.nextInt();    //테스트 케이스의 개수 입력 받기
        
        for(int i=1; i<=x;i++) {
            int a=s.nextInt();    //A 입력 받기
            int b=s.nextInt();    //B 입력 받기
            
            System.out.println(a+b);    //A+B 출
        }
                
    }
}
 
cs

 

8393번- 합
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        
        int a=s.nextInt();    
        int sum=0;
        
        for(int i=1; i<=a;i++) {
            sum+=i;
        }
        
        System.out.println(sum);
    }
}
 
cs
15552번- 빠른 A+B
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
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
public class Main{
    public static void main(String[] args) throws IOException {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        int x = Integer.parseInt(br.readLine());
 
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        
        for (int i = 1; i <= x; i++) {
            String s=br.readLine();
            int a = Integer.parseInt(s.split(" ")[0]);
            int b = Integer.parseInt(s.split(" ")[1]);
            
            
            bw.write(String.valueOf(a + b));
            bw.newLine();
        }
        br.close();
        bw.flush();
        bw.close();
    }
}
 
cs

 

2741번 - N찍기
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 s = new Scanner(System.in);
 
        int a = s.nextInt();
 
        for (int i = 1; i <= a; i++) {
            System.out.println(i);
        }
    }
}
 
cs

 

2742번 - 기찍 N
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 s = new Scanner(System.in);
 
        int n = s.nextInt(); // 자연수 N 입력받기
 
        for (int i = n; i >= 1; i--) { // n~1까지 하나씩 출력하기
            System.out.println(i);
        }
    }
 
}
 
cs

 

11021번 - A+B -7

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
 
        int t = s.nextInt(); // 테스트 케이스의 개수 T 입력 받기
        s.nextLine();
 
        for (int i = 1; i <= t; i++) {
            String in = s.nextLine();
            int a = Integer.parseInt(in.split(" ")[0]);
            int b = Integer.parseInt(in.split(" ")[1]);
 
            System.out.println("Case #" + i + ": " + (a + b));
        }
    }
}
 
s

 

11022번 - A+B -8

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.

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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
        int t=Integer.parseInt(br.readLine());
        
        for(int i=1; i<=t;i++)
        {
            String in=br.readLine();
            int a=Integer.parseInt(in.split(" ")[0]);
            int b=Integer.parseInt(in.split(" ")[1]);
            
            bw.write(String.valueOf("Case #"+i+": "+a+" + "+b+" = "+(a+b)));
            bw.newLine();
        }
        
        bw.flush();
        br.close();
        bw.close();
        
    }
}
 
cs

 

2438번 - 별 찍기 - 1

첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제

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.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 
        int n = Integer.parseInt(br.readLine());
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                bw.write("*");
            }
            bw.newLine();
        }
        br.close();
        bw.flush();
        bw.close();
    }
}
 
cs

 

2439번 - 별 찍기 - 2

첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제

하지만, 오른쪽을 기준으로 정렬한 별(예제 참고)을 출력하시오.

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.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 
        int n = Integer.parseInt(br.readLine()); // N 읽기
 
        for (int i = 1; i <= n; i++) {
            for (int x = (n - i); x >0; x--) {
                bw.write(" ");
            }
            for (int j = 1; j <= i; j++) {
 
                bw.write("*");
            }
            bw.newLine();
        }
        br.close();
        bw.flush();
        bw.close();
    }
 
}
cs

 

10871번 - X보다 작은 수 

정수 N개로 이루어진 수열 A와 정수 X가 주어진다. 이때, A에서 X보다 작은 수를 모두 출력하는 프로그램을 작성하시오.

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
import java.io.*;
 
public class N10871 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 
        String in = br.readLine();
 
        int n = Integer.parseInt(in.split(" ")[0]);
        int x = Integer.parseInt(in.split(" ")[1]);
 
        String in2 = br.readLine();
        for (int i = 0; i < n; i++) {
            int out = Integer.parseInt(in2.split(" ")[i]);
            if (out < x) {
                bw.write(out + " ");
            }
        }
 
        br.close();
        bw.flush();
        bw.close();
 
    }
}
 
cs

 

10952번 - A+B - 5

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.

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
 
import java.io.*;
 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 
        boolean state = true;
 
        while (state) {
            String in = br.readLine();
            int a = Integer.parseInt(in.split(" ")[0]);
            int b = Integer.parseInt(in.split(" ")[1]);
 
            if (a != 0 && b != 0) {
                bw.write((a + b) + "\n");
            } else {
                state = false;
            }
        }
        br.close();
        bw.flush();
        bw.close();
    }
}
 
cs

 

10951번 - A+B - 4

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오. (EOF에 대해 알아보자)

 

>참고

2022.04.28 - [알고리즘/백준] - [java, 백준]10951번: A+B -4 //EOF 오류 방지

 

[java, 백준]10951번: A+B -4 //EOF 오류 방지

EOF란? 컴퓨팅에서, 파일 끝(End of File, EOF)은 데이터 소스로부터 더 이상 읽을 수 있는 데이터가 없음을 나타낸다. 알고리즘에서 주로 입력값을 명시하지 않을 경우 사용합니다. 10951번 문제 두 정

kjk04021.tistory.com

 

1) Scanner 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
 
public class Main{
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
 
        while (s.hasNextInt()) {
 
            int a = s.nextInt();
            int b = s.nextInt();
 
            System.out.println(a + b);
        }
        s.close();
    }
}
 
cs

 

2) Buffer 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.*;
 
public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        
        String in;
        while((in=br.readLine()) != null) {
            int a=Integer.parseInt(in.split(" ")[0]);
            int b=Integer.parseInt(in.split(" ")[1]);
            System.out.println(a+b);
        }
        br.close();
        
    }
}
 
cs

 

10951번 - 더하기 사이클

원래 수로 돌아올 때까지 연산을 반복하는 문제

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.*;
 
public class Main{
    public static void main(String[] args) throws IOException {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        int n = Integer.parseInt(br.readLine()); // N 입력받
 
        int p = n; // 새로운 수
        int cnt = 0// 사이클의 길이
 
        while (true) {
 
            int a = p / 10;
            int b = p % 10;
 
            p = (b * 10+ (a + b) % 10;
            cnt++;
            if (p == n) {
                break;
            }
        }
        br.close();
        System.out.println(cnt);
    }
}
 
cs

 

출처:[백준]https://www.acmicpc.net/step/3

 

반복문 단계

1부터 N까지의 합을 구하는 문제. 물론 반복문 없이 풀 수도 있습니다.

www.acmicpc.net

 

728x90
반응형