Baekjoon_Step2

주가희·2023년 10월 4일

1. 두 수 비교하기

import java.util.Scanner;

public class Ex01 {

	public static void main(String[] args) {
		int A,B = 0;
		
		Scanner sc = new Scanner(System.in);
		
		A = sc.nextInt();
		B = sc.nextInt();
		
		if(A>B)
			System.out.println(">");
		else if(A<B)
			System.out.println("<");
		else
			System.out.println("==");
	}

}

2. 시험 성적

import java.util.Scanner;

public class Ex02 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int num = sc.nextInt();
		
		if(num >= 90 && num <=100)
			System.out.println("A");
		else if(num >= 80 && num <90)
			System.out.println("B");
		else if(num >= 70 && num <80)
			System.out.println("C");
		else if(num >= 60 && num <70)
			System.out.println("D");
		else
			System.out.println("F");
	}

}

3. 윤년


import java.util.Scanner;

public class Ex03 {

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

}

4. 사분면 고르기

import java.util.Scanner;

public class Ex04 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int x = sc.nextInt();
		int y = sc.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");
		
	}

}

5. 알람시계


import java.util.Scanner;

public class Ex05 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int h = sc.nextInt();	//시를 입력 받을 변수
		int m = sc.nextInt();	//분을 입력 받을 변수
		sc.close();
		
		if (m>=45) {
			System.out.println(h + " " + (m-45));	//분이 45보다 작거나 같다면 분에서 45를 뺀 값을 출력
		}
		else if(h !=0 && h<=23 && m<45) {
			System.out.println((h-1) + " " + (m+15));	//분이 45분 보다 작다면 시에 -1, 분에 +15 를 해준다 
		}
		else if(h==0 && m<45 ) {
			h=23;
			System.out.println(h + " " + (m+15));	//시가 23이고 분이 45보다 작다면 h를 0으로 초기화한다
		}
		
	}

}

예제와 출력은 같지만 틀렸습니다! 가 나온다

조건식을 좀 더 논리적으로 생각하고 써야겠다..



import java.util.Scanner;

public class Ex05 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int h = sc.nextInt();	//시를 입력 받을 변수
		int m = sc.nextInt();	//분을 입력 받을 변수
		sc.close();
		
		if(h == 0 && m <45) {
				h =23;
				System.out.println(h + " " + (m + (60-45)));
		}
		else if(m<45)
			System.out.println((h-1) + " " + (m + (60-45)));
		else if(m>=45)
			System.out.println(h + " " + (m-45));
	}

}

코드를 수정하고도 계속 "틀렸습니다"가 나왔지만 m ==45 일때 처리를 안해서 그런거였다
마지막 else if 조건에 =을 넣었더니 정답!

6. 오븐 시계

import java.util.Scanner;

public class Ex06 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int h = sc.nextInt();	//시간
		int m = sc.nextInt();	//분
		int c = sc.nextInt();	//조리시간
		
		if(h == 23 && (m+c)>=60) {	//23시이고 분+조리(분)이 60보다 클 때
			h = 0;					//25시로 넘어가므로 h를 0으로 초기화
			System.out.println(h + " " + (m+c-60));
		}
		//m(분) + c(조리시간)이 59를 넘지 않아서 h의 수정이 필요없는 경우
		else if((m+c)<60) {	
			System.out.println(h + " " + (m+c));
		}
	
		else if(h<=22 && (m+c)>=60) {	//h(시)가 22이하이면서 m+c가 60이상인 경우
			//m+c를 60으로 나눈 몫을 h에 더하고 m+c을 60으로 나눈 나머지를 남은 분으로 출력
			System.out.println((h+((m+c)/60)) + " " + ((m+c)%60));
		}
	}

}

[접근 방법]

  1. h(시)와 m(분)을 분단위로 변환한 후 c(조리시간)을 더하고 다시 시,분으로 변환하는 방법
  2. c(조리시간)을 시와 분으로 변한환 후 h와 m에 각각 더해주기

이런식으로 새롭게 생각하고 접근하는법이 아직 많이 부족한것 같다.

입력값에 대한 설명에 빈칸을 사이에 두고 , 두 번째 줄에는 이라는 문장이 있는데 다른 사람들의 풀이를 보다보니 이것도 Scanner 대신 BufferedReader를 사용하는 방식도 있다고 하고 StringTokenizer? 입력을 사용하는 것이 훨씬 빨라보인다
String 함수인것 같다

import java.util.Scanner;

public class Ex06 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int h = sc.nextInt();	//시간
		int m = sc.nextInt();	//분
		int c = sc.nextInt();	//조리시간
		
		int cookTime = (h*60)+m+c; //입력 받은 h 와 m을 분으로 환산하고 조리시간을 더함
		h = cookTime/60;	//h는 cookTime을 60으로 나눈 몫
		m = cookTime%60;	//m은 cookTime을 60으로 나눈 나머지
		if(h>=23) {
			h=0;	//h가 23시 라면 0으로 초기화 시켜준다
		}
		System.out.println(h + " " + m);
		
	}
}

여기서 틀린이유 : h가 23초과가 되는 것을 고려하지 않아서

import java.util.Scanner;

public class Ex06 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int h = sc.nextInt();	//시간
		int m = sc.nextInt();	//분
		int c = sc.nextInt();	//조리시간
		
		int cookTime = (h*60)+m; //입력 받은 h 와 m을 분으로 환산
		cookTime +=c;		//
		h = (cookTime/60)%24;	//h는 cookTime을 60으로 나눈 몫
		m = cookTime%60;	//m은 cookTime을 60으로 나눈 나머지
		System.out.println(h + " " + m);
		
	}
}

생각보다 코드가 간단해서 놀랐다.
그런데 난 왜 그렇게 복잡하게 풀려고 했을까 생각하다가 알게되었다
문제가 조건문 챕터에 들어가있어서 처음에 if문에 그렇게 집착했나보다
근데 그럼 조건문으로 풀어야되는거 아닌가

7. 주사위 세개

package baekjoon2;

import java.util.Scanner;

public class Ex07 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int dice1 = sc.nextInt();
		int dice2 = sc.nextInt();
		int dice3 = sc.nextInt();
		
		int money;	//상금을 저장할 변수

		
		
		if(dice1 != dice2 && dice2 != dice3 && dice1 != dice3) {
			// 모두 다른 눈이 나오는 경우
			// 제일 큰 수 구하기
			int max = Math.max(dice1, Math.max(dice2, dice3));
			
			money = 100 * max;
		}else if(dice1 == dice2 && dice2 == dice3 && dice1 == dice3) {
			// 모두 같은 눈이 나오는 경우
			money = 10000 + dice1 * 1000;
		}else {
			//같은 눈이 2개만 나오는 경우
			// 같은 두 수 구하기
			int same;	//같은 수를 저장할 변수
			if(dice1==dice2) {
				same = dice1;
			}else if(dice2==dice3){
				same = dice2;
			}else {
				same = dice3;
			}
			money = 1000+same*100;
		}
		
		System.out.println(money);


		
	}

}
profile
주웅

0개의 댓글