본문 바로가기
Algorithm/백준(BOJ)

백준 3단계 - Java

by k-mozzi 2023. 4. 2.
반응형
Preface

 

3단계는 반복문과 관련된 문제들이다.

 

문제 자체의 난이도는 평이했지만, System.out.print( )를 사용하는 출력 방법 외에 Scanner, BufferedReader 등의 사용 방법을 익히는 것이 조금 복잡했다.

 

Scanner를 이용하는 방법은 꽤 익숙해졌지만, BufferedReader와 StringTokenizer의 사용 방법은 아직 완벽히 이해하지 못했다.

 

본문에 달아둔 링크의 글을 여러번 읽어보며 해당 개념을 제대로 이해하고 넘어갈 생각이다.

 

또, 이번 주말에 정보처리기능사 필기 시험이 있어 이번 주는 시험 준비를 해야 할 것 같다.

 

시험을 본 이후부턴 월요일엔 백준 문제를, 다른 날에는 이것이 자바다 책을 공부할 계획이다.


 

https://www.acmicpc.net/step/3

 

반복문 단계

코딩 공부를 잘 하여 이렇게 long long long long...을 칠판에 적는 일이 없도록 합시다.

www.acmicpc.net

 

 

- 2739번: 구구단

package num3;

import java.util.Scanner;

public class B2739 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		int A = scanner.nextInt();
		scanner.close();
		
		for(int i = 1; i <=9; i++) {
			System.out.println(A + " * " + i + " = " + A * i);
		}
	}

}

 

 

- 10950번: A+B - 3

package num3;

import java.util.Scanner;

public class B10950 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int A = scanner.nextInt();
		int arr[] = new int[A];
		
		for(int i = 0; i < A; i++) { 
			int a = scanner.nextInt();
			int b = scanner.nextInt();
			arr[i] = a + b;
		}
		scanner.close();

		for(int k : arr) {
			System.out.println(k);
		}
	}

}

 

 

- 8393번: 합

package num3;

import java.util.Scanner;

public class B8393 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int A = scanner.nextInt();
		scanner.close();
		int sum = 0;

		for (int i = 1; i <= A; i++) {
			sum += i;
		}
		System.out.println(sum);
	}

}

 

 

- 25304번: 영수증

package num3;

import java.util.Scanner;

public class B25304 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int X = scanner.nextInt();
		int N = scanner.nextInt();
		int sum = 0;

		for (int i = 0; i < N; i++) {
			int a = scanner.nextInt();
			int b = scanner.nextInt();
			sum += a * b;
		}
		scanner.close();

		if (X == sum) {
			System.out.println("Yes");
		} else {
			System.out.println("No");
		}
	}

}

 

 

- 25314번: 코딩은 체육과목 입니다.

package num3;

import java.util.Scanner;

public class B25314 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		scanner.close();
		int counter = N / 4;
		String str = "long ";

		if (N % 4 == 0) {
			for (int i = 0; i < counter; i++) {
				System.out.print(str);
			}
			System.out.println("int");
		} else {
			for (int i = 0; i <= counter; i++) {
				System.out.print(str);
			}
			System.out.println("int");
		}
	}

}

 

 

- 15552번: 빠른 A+B

package num3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class B15552 {

	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());

		StringTokenizer st;

		for (int i = 0; i < N; i++) {
			st = new StringTokenizer(br.readLine());
			bw.write((Integer.parseInt(st.nextToken()) + Integer.parseInt(st.nextToken())) + "\n");
		}
		br.close();

		bw.flush();
		bw.close();

	}
}

//public class B15552 {
//
//	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 = 0; i < n; i++) {
//			String s = br.readLine();
//			int a = Integer.parseInt(s.split(" ")[0]);
//			int b = Integer.parseInt(s.split(" ")[1]);
//			bw.write(a + b + "\n");
//		}
//		br.close();
//		bw.flush();
//		bw.close();
//	}
//
//}

→ 참고 블로그 1: https://st-lab.tistory.com/30

 

[백준] 15552번 : 빠른 A+B - JAVA [자바]

https://www.acmicpc.net/problem/15552 15552번: 빠른 A+B 첫 줄에 테스트케이스의 개수 T가 주어진다. T는 최대 1,000,000이다. 다음 T줄에는 각각 두 정수 A와 B가 주어진다. A와 B는 1 이상, 1,000 이하이다. www.acmic

st-lab.tistory.com

→ 참고 블로그 2(BufferedReader 설명): https://chloe-ki.tistory.com/entry/java-bufferedreader-and-bufferedwriter-methods-and-exception-handling

 

자바 bufferedreader & writer 사용법과 IOException (백준 15552번)

Scanner를 사용할 때는 java.util 패키지를 임포트하지만, BufferedReader/Writer는 java.io 패키지를 임포트해야한다. 이 패키지 안에는 I/O(입출력)와 관련된 여러 클래스들이 존재하는데, 그 중 버퍼를 활용

chloe-ki.tistory.com

 

 

- 11021번: A+B - 7

package num3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class B11021 {

	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());

		StringTokenizer st;

		for (int i = 0; i < N; i++) {
			st = new StringTokenizer(br.readLine());
			bw.write("Case #" + (i + 1) + ": " + (Integer.parseInt(st.nextToken()) + Integer.parseInt(st.nextToken())) + "\n");
		}
		br.close();

		bw.flush();
		bw.close();
	}

}

 

 

- 11022번: A+B - 8

package num3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
//import java.util.StringTokenizer;

public class B11022 {

	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 = 0; i < N; i++) {
			String s = br.readLine();
			int a = Integer.parseInt(s.split(" ")[0]);
			int b = Integer.parseInt(s.split(" ")[1]);
			bw.write("Case #" + (i + 1) + ": " + a + " + " + b + " = " + (a + b) + "\n");
		}
		br.close();

		bw.flush();
		bw.close();
	}

}


//public class Main {
//	public static void main(String args[]) throws IOException {
// 
//		BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
//		
//		int T = Integer.parseInt(br.readLine());
//		int A;
//		int B;
//        
//		StringTokenizer st;
//		for (int i = 1; i <= T; i++) {
//			st = new StringTokenizer(br.readLine()," ");
//			A = Integer.parseInt(st.nextToken());
//			B = Integer.parseInt(st.nextToken());
//			System.out.println("Case #" + i + ": " + A + " + " + B + " = " + (A+B));
//		}
//		br.close();
//	}
// 
//}

 

 

- 2438번: 별 찍기 - 1

package num3;

import java.util.Scanner;

public class B2438 {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		scanner.close();
		String s = "*";

		for (int i = 0; i < N; i++) {
			System.out.println(s);
			s += "*";
		}
	}

}

 

 

- 2439번: 별 찍기 - 2

package num3;

import java.util.Scanner;

public class B2439 {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		scanner.close();

		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N - i; j++) {
				System.out.print(" ");
			}
			for (int k = 0; k < i; k++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}

}

 

 

- 10952번: A+B - 5

package num3;

import java.util.Scanner;

public class B10952 {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		while (true) {

			int A = scanner.nextInt();
			int B = scanner.nextInt();

			if (A == 0 && B == 0) {
				scanner.close();
				break;
			}
			System.out.println(A + B);
		}
	}

}

 

 

- 10951번: A+B - 4

package num3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class B10951 {
	public static void main(String args[]) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		StringTokenizer st;
		String str;
 
		while( (str=br.readLine()) != null ){
		    
			st = new StringTokenizer(str," ");
			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			sb.append(a+b).append("\n");
		
		}
		System.out.print(sb);
	}
}


//public class B10951 {
//
//	public static void main(String[] args) {
//
//		Scanner scanner = new Scanner(System.in);
//
//		while (scanner.hasNextInt()) {
//
//			int a = scanner.nextInt();
//			int b = scanner.nextInt();
//			System.out.println(a + b);
//		}
//		scanner.close();
//	}
//
//}

→ 참고 블로그: https://st-lab.tistory.com/40

 

[백준] 10951번 : A+B - 4 - JAVA [자바]

https://www.acmicpc.net/problem/10951 10951번: A+B - 4 두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오. www.acmicpc.net 문제 간단한 문제지만 의외로 종료시점을 몰라 틀리는 경우들이 많

st-lab.tistory.com

 

728x90
반응형

'Algorithm > 백준(BOJ)' 카테고리의 다른 글

백준 6단계 - Java  (0) 2023.05.22
백준 5단계 -Java  (2) 2023.04.18
백준 4단계 - Java  (0) 2023.04.11
백준 2단계 - Java  (0) 2023.03.23
백준 1단계 - Java  (0) 2023.03.22

댓글