본문 바로가기

반응형

알고리즘/백준

(111)
[백준] 9498 - 시험 성적 조건문 하면 빠지지 않고 나오는 예제 중에 하나다. if~else 문을 이용하는 방법과 switch~case 문을 이용해서 푸는 방법이 있다. 1. if문을 이용하는 방법 import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int score = sc.nextInt(); if(score >= 90 && score = 80 && score = 70 && score = 60 && scor..
[백준] 1330 - 두 수 비교하기 백준을 단계별로 풀고 있는데 드디어 1단계인 입출력과 사칙연산을 모두 풀어서 이제 2단계인 조건문으로 들어왔다! 저번에 BufferedReader로 푸는 법을 해봤기 때문에 이제 Scanner로 푸는 방법과 BufferedReader로 푸는 법 2가지를 써서 풀어보려고 한다. 기본적으로 푸는 법은 같기 때문에 어렵지 않을 것 같다. 1. Scanner 클래스로 푸는 법 import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int a, b; a = sc.nextInt(); b = sc.nextInt(); if(a > b){ System.out.printl..
[백준] 10172 - 개 이전의 고양이 문제와 똑같은 방식으로 풀면 된다. public class Main{ public static void main(String[] args){ System.out.println("|\\_/|"); System.out.println("|q p| /}"); System.out.println("( 0 )\"\"\"\\"); System.out.println("|\"^\"` |"); System.out.println("||_/=\\\\__|"); } }
[백준] 10171 - 고양이 간단히 system.out.println() 을 사용하고, \는 \\ 2개 붙여서 사용하면 되는 문제다. 예제 출력에 있는 것을 그대로 복사해서 붙여넣어야 정답 처리가 된다. 일일히 치고 간격 조정하고 했다가 틀렸다.. public class Main{ public static void main(String[] args){ System.out.println("\\ /\\"); System.out.println(" ) ( ')"); System.out.println("( / )"); System.out.println(" \\(__)|"); } } System.out.println() 은 애초에 자동으로 줄바꿈을 해주는 코드인데 이전에 푼 백준 문제들에 따로 또 줄바꿈 코드를 넣어줬다는 걸 이제서야 깨달았다..
[백준] 11382 - 꼬마 정민 10¹²까지 입력할 수 있어야하므로 int가 아닌 long 타입으로 받아와야 한다. import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); long a, b, c; a = sc.nextLong(); b = sc.nextLong(); c = sc.nextLong(); System.out.println(a+b+c); sc.close(); } } 이전에는 계속 while문으로 조건을 체크했었는데, 조건 체크를 채점 시에 따지지도 않고 굳이 하지 않아도 될 것 같아서 while문을 쓰지 않고 풀었다.
[백준] 2588 - 곱셈 원래는 숫자를 하나씩 뽑아서 계산하는 방식으로 하려고 했는데 그러려면 문자열로 받아서 해야한다고 한다. 우선 나는 계산 방식으로 풀고 숫자를 하나씩 뽑아서 계산하는 방식을 풀게 되면 추가로 올리려고 한다. import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int a, b; a = sc.nextInt(); b = sc.nextInt(); while(a > 999 || b > 999){ System.out.println("a, b는 3자리 이하여야합니다."); a = sc.nextInt(); b = sc.nextInt(); } System.out.pri..
[백준] 10430 - 나머지 import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int a, b, c; a = sc.nextInt(); b = sc.nextInt(); c = sc.nextInt(); while(a 10000 || b > 10000 || c > 10000){ System.out.println("a, b, c의 값은 2 이상 10000이하여야 합니다."); a = sc.nextInt(); b = sc.nextInt(); c = sc.nextInt(); } System.out.println((a+b)%c); Syst..
[백준] 18108 - 1998년생인 내가 태국에서는 2541년생?! 태국력은 서기 연도에 543년을 더하면 된다고 한다. 예제의 2541년에서 1998년을 빼서 알아내는 방법도 있다. import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int y; y = sc.nextInt(); while(y 3000){ System.out.println("1000이상 3000이하로 다시 입력하세요!"); y = sc.nextInt(); } System.out.println(y - 543); sc.close(); } }

반응형