Algorithm

[Java]백준-문제-단계별로 풀어보기-2

isaac.kim 2022. 4. 11.
728x90
반응형

[Java]백준-문제-단계별로 풀어보기-2

 

문제 : 1330

제목 : 두 수 비교하기

출력 : 

두 정수 A, B를 비교하여 다음을 출력한다.

첫째 줄에 다음 세 가지 중 하나를 출력한다.

  • A가 B보다 큰 경우에는 '>'를 출력한다.
  • A가 B보다 작은 경우에는 '<'를 출력한다.
  • A와 B가 같은 경우에는 '=='를 출력한다.
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		// 두 정수 A와 B를 비교하는 프로그램 작성
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		if (a > b) System.out.println(">");
		else if (a < b) System.out.println("<");
		else System.out.println("==");
		scanner.close();
	}
}

 

 

문제 : 9498

제목 : 시험 성적

시험 점수를 입력 받아 A, B, C, D, F 등급 나누기.

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int p = scanner.nextInt();
		
		if (p >= 90 && p <= 100 ) System.out.println("A");
		else if (p >= 80 && p <= 89 ) System.out.println("B");
		else if (p >= 70 && p <= 79 ) System.out.println("C");
		else if (p >= 60 && p <= 69 ) System.out.println("D");
		else System.out.println("F");
		
		scanner.close();
	}
}

 

 

문제 : 2753

제목 : 윤년

 

윤년이면 1, 아니면 0 출력

윤년은 연도가 4의 배수이면서, 100의 배수가 아닐 때 또는 400의 배수일 때이다.

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int year = scanner.nextInt();
		
		if ( year % 4 == 0 ) {
			if ( year % 100 != 0 || year % 400 == 0 ) System.out.println(1);
			else System.out.println(0);
		} else System.out.println(0);
		
		scanner.close();
	}
}

 

 

문제 : 14681

제목 : 사분면 고르기

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int x = scanner.nextInt();
		int y = scanner.nextInt();
		
		if (x > 0 && y > 0) System.out.println(1);
		else if (x < 0 && y > 0) System.out.println(2);
		else if (x < 0 && y < 0) System.out.println(3);
		else if (x > 0 && y < 0) System.out.println(4);

		scanner.close();
	}
}

 

 

문제 : 2884

제목 : 알람 시계

 

24시간 표현에서 시간이 주어졌을 때 45분 전 시간을 출력할 것.

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int h = scanner.nextInt();
		int m = scanner.nextInt();
		
		if (m - 45 < 0) {
			m = 60 + (m - 45);
			h = h - 1;
			if (h < 0) h = 23;
		} else m = m - 45;
		
		System.out.println(h+" "+m);
		scanner.close();
	}
}

 

 

문제 : 2525

제목 : 오븐 시계

입력 : 첫째 줄에는 현재 시각이 나온다. 현재 시각은 시 A (0 ≤ A ≤ 23) 와 분 B (0 ≤ B ≤ 59)가 정수로 빈칸을 사이에 두고 순서대로 주어진다. 두 번째 줄에는 요리하는 데 필요한 시간 C (0 ≤ C ≤ 1,000)가 분 단위로 주어진다. 

출력 : 첫째 줄에 종료되는 시각의 시와 분을 공백을 사이에 두고 출력한다. (단, 시는 0부터 23까지의 정수, 분은 0부터 59까지의 정수이다. 디지털 시계는 23시 59분에서 1분이 지나면 0시 0분이 된다.)

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int h = scanner.nextInt();
		int m = scanner.nextInt();
		int timer = scanner.nextInt(); 
		
		if ( m + timer > 59) {
			h += (m + timer) / 60;
			m = (m + timer) % 60;
			if (h > 23) h %= 24;
		} else m += timer;
			
		System.out.println(h + " " + m);
		
		scanner.close();
	}
}

 

 

문제 : 2480

제목 : 주사위 세개

1. 같은 눈이 3개 나오면 10,000원 + (같은 눈) x 1,000원

2. 같은 눈이 2개 나오면 1,000원 + (같은 눈) x 100원

3. 모두 다른 눈이 나오면 (가장 큰 눈) x 100원

 

입력 : 첫째 줄에 3개의 눈이 빈칸을 사이에 두고 각각 주어진다.

출력 : 첫째 줄에 게임의 상금을 출력 한다.

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		int c = scanner.nextInt(); 
		int res = 0;
		
		// 모두 같은 경우
		if ( a == b && b == c ) res = 10000 + a * 1000;
		// 모두 다른 경우
		else if (a != b && a != c && b != c) {
			// (가장 큰 눈) * 100
			if ( a > b && a > c ) res = a * 100;
			else if ( b > a && b > c) res = b * 100;
			else if (c > a && c > b) res = c * 100;
		} else { // 같은 눈 2개 
			// 1000 + (같은 눈) * 100  
			if ( a == b ) res = 1000 + a * 100;
			else if ( a == c ) res = 1000 + a * 100;
			else if ( b == c ) res = 1000 + b * 100;
		}
		System.out.println(res);
		scanner.close();
	}
}

 

 

 


 

728x90
반응형