Baekjoon

[자바] 백준 - 3009번, 4153번, 2557번

yujin0517 2021. 7. 29. 11:54

3009번 - 네 번째 점

 

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] num1 = {sc.nextInt(), sc.nextInt()};
		int[] num2 = {sc.nextInt(), sc.nextInt()};
		int[] num3 = {sc.nextInt(), sc.nextInt()};
		sc.close();
		
		int x = 0, y = 0;
		
		if(num1[0] == num2[0]) {
			x = num3[0];
		} else if(num1[0] == num3[0]) {
			x = num2[0];
		} else if(num2[0] == num3[0]) {
			x = num1[0];
		}
		
		if(num1[1] == num2[1]) {
			y = num3[1];
		} else if(num1[1] == num3[1]) {
			y = num2[1];
		} else if(num2[1] == num3[1]) {
			y = num1[1];
		}
		
		System.out.println(x + " " + y);
	}

}

처음에 사각형의 중점을 이용하는 문제인 줄 알고 열심히 계산했지만, 결국은 x, y좌표를 이용하는 문제였다고 합니다..

직각 사각형의 평행이라는 특징을 아예 사용하지 않고 있었네요..

다시 평행을 사용해 조건문을 작성하여 문제를 풀었습니다!

 

4153번 - 직각삼각형

 

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		while (true) {
			int Auset = sc.nextInt();
			int Ausar = sc.nextInt();
			int Heru = sc.nextInt();

			if (Auset == 0 && Ausar == 0 && Heru == 0) {
				break;
			}
			
			if (Auset * Auset == (Ausar * Ausar + Heru * Heru)) {
				System.out.println("right");
			} else if (Ausar * Ausar == (Auset * Auset + Heru * Heru)) {
				System.out.println("right");
			} else if (Heru * Heru == (Auset * Auset + Ausar * Ausar)) {
				System.out.println("right");
			} else {
				System.out.println("wrong");
			}
		}
		sc.close();
	}

}

이 문제를 풀 때 했던 실수는 빗변을 정해놓고 코드를 작성했다는 것입니다. 

3개의 변 중에서 "어느 변이든 빗변이 될 수 있다"를 조건문으로 작성하였습니다. 

 

2577번 - 숫자의 개수 

 

<Scanner 사용>

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int num = sc.nextInt() * sc.nextInt() * sc.nextInt();
		String str = Integer.toString(num);

		sc.close();

		for (int i = 0; i < 10; i++) {
			int count = 0;
			for (int j = 0; j < str.length(); j++) {
				if ((str.charAt(j) - '0') == i) {
					count++;
				}
			}
			System.out.println(count);
		}
	}

}

 

<BufferedReader, InputStreamReader, IOException 사용>

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

public class bj_2577_2 {

	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int[] arr = new int[10];
		
		int value = Integer.parseInt(br.readLine()) * Integer.parseInt(br.readLine()) * Integer.parseInt(br.readLine());
		
		String str = String.valueOf(value);
		
		for(int i = 0; i < arr.length; i++) {
			int count = 0;
			for(int j = 0; j < str.length(); j++) {
				if((str.charAt(j) - '0') == i) {
					count++;
				}
			}
			System.out.println(count);
		}
	}

}

이 문제를 풀면서 래퍼 클래스, charAt 메소드를 공부하였습니다. 아직 익숙지 않지만 더 연습해야겠습니다.

 

앞으로는 더 자주 백준 문제 풀이 글을 올리도록 하겠습니다.

 

감사합니다!

 

2021.07.29