자바

[JAVA] 백준 기초공부 2

코딩하는둥이 2022. 11. 19. 18:03

백준 14681번: 사분면 고르기

 

1. 문제 

https://www.acmicpc.net/problem/14681

 

14681번: 사분면 고르기

점 (x, y)의 사분면 번호(1, 2, 3, 4 중 하나)를 출력한다.

www.acmicpc.net

 

2. 코딩

import java.util.Scanner;

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

 

 

백준 2525번 오븐시계

 1. 문제

https://www.acmicpc.net/problem/2525

 

2525번: 오븐 시계

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

www.acmicpc.net

2. 코딩

import java.util.Scanner;

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

    int h = sc.nextInt();
    int m = sc.nextInt();
    int mm = sc.nextInt();

    h += mm / 60;  // 60분 이상이면 넘는 만큼 h 더함
    m += mm % 60;  // 남은 분을 m에 더함

    if (m >= 60){
    	h += 1;    // m이 60 이상이면 h룰 한시간 더함
    	m -= 60;   // m은 60을 -한다.
    }
    if (h >= 24){
    	h -= 24;   // h가 24시 이상이면 24를 빼줌
    }
      
    System.out.println (h + " " + m);

  }
}

 

백준 2480번 주사위 세 개

1. 문제

https://www.acmicpc.net/problem/2480

 

2480번: 주사위 세개

1에서부터 6까지의 눈을 가진 3개의 주사위를 던져서 다음과 같은 규칙에 따라 상금을 받는 게임이 있다.  같은 눈이 3개가 나오면 10,000원+(같은 눈)×1,000원의 상금을 받게 된다.  같은 눈이 2개

www.acmicpc.net

2. 코딩

import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
 
		int A = in.nextInt();
		int B = in.nextInt();
		int C = in.nextInt();
 
		// 만약 모든 변수가 다른 경우일때
		if (A != B && B != C && A != C) {
			int max;
			if (A > B) {
				if (C > A) {
					max = C;
				} 
				// A > (B, C)
				else {
					max = A;
				}
			}
			// B > A 라면	
			else {
				if (C > B) {
					max = C;
				}
				// B > (A, C)
				else {
					max = B;
				}
			}
			System.out.println(max * 100);
		}
		// 적어도 한 쌍 이상의 서로 같은 변수가 존재할 경우
		else {
			if (A == B && A == C) {
				System.out.println(10000 + A * 1000);
			}
			else {
				if(A == B || A == C) {
					System.out.println(1000 + A * 100);
				}
				// B=C 같은 경우
				else {
					System.out.println(1000 + B * 100);
				}
			}
		}
	}
}

백준 2739번 구구단

1. 문제

https://www.acmicpc.net/problem/2739

 

2739번: 구구단

N을 입력받은 뒤, 구구단 N단을 출력하는 프로그램을 작성하시오. 출력 형식에 맞춰서 출력하면 된다.

www.acmicpc.net

2. 코딩

import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
 
		int A = in.nextInt();
		
		in.close();

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

	}
}

백준 10871번 X보다 작은 수

1. 문제

https://www.acmicpc.net/problem/10871

 

10871번: X보다 작은 수

첫째 줄에 N과 X가 주어진다. (1 ≤ N, X ≤ 10,000) 둘째 줄에 수열 A를 이루는 정수 N개가 주어진다. 주어지는 정수는 모두 1보다 크거나 같고, 10,000보다 작거나 같은 정수이다.

www.acmicpc.net

2. 코딩

import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
 
		int N = in.nextInt();
		int X = in.nextInt();
		int arr[] = new int[N];
        
		for (int i = 0; i < N; i++) {
			arr[i] = in.nextInt();
		}
 
		in.close();
        
		for (int i = 0; i < N; i++) {
			if (arr[i] < X) {
				System.out.print(arr[i] + " ");
			}
		}
	}
}

'자바' 카테고리의 다른 글

[JAVA] 배열  (0) 2022.11.26
[JAVA] 변수와 자료형  (0) 2022.11.19
[JAVA] 클래스와 객체2  (0) 2022.11.19
[JAVA] 클래스와 객체 1  (0) 2022.11.19
[JAVA] 객체와 클래스  (0) 2022.11.19