목록알고리즘 (23)
동그란 도그린
입력 아홉 개의 줄에 걸쳐 난쟁이들의 키 입력 (100 이하의 자연수, 중복 X) 출력 일곱 난쟁이의 키를 오름차순으로 출력 (가능한 정답이 여러 가지인 경우에는 아무거나 출력, 일곱 난쟁이를 찾을 수 없는 경우는 없음) 풀이방법 Collections.sort()를 이용하여 난쟁이들의 키가 저장된 ArrayList를 오름차순으로 정렬 난쟁이 아홉 명 키의 합계에서 난쟁이 두 명의 키를 뺀 값이 100인 경우를 찾음 for문을 이용하여 탐색하다가 위에 해당하는 경우를 찾으면, 리스트에서 난쟁이 두 명의 키를 삭제하고 반복문을 종료 코드 import java.io.*; import java.util.*; public class BOJ_2309 { public static void main(String[] a..
입력 첫째 줄에 세 수 E, S, M 입력 (1 ≤ E ≤ 15, 1 ≤ S ≤ 28, 1 ≤ M ≤ 19) 출력 첫째 줄에 E S M으로 표시되는 가장 빠른 연도 출력 풀이 방법 연도(year)를 E+15i 혹은 S+28j 혹은 M+19k로 표현 가능 (i, j, k는 0 이상의 정수) 위를 통해 아래의 두 식을 도출 가능 j=(year-S) / 28 k=(year-M) / 19 이때 j와 k는 0 이상의 정수이므로 아래의 조건을 만족해야 함 (year-S)≥0 && (year-S)%28==0 (year-M)≥0 && (year-M)%19==0 코드 import java.io.*; import java.util.*; public class BOJ_1476 { public static void main(..
입력 첫 줄에 입력 받을 수의 개수 N 입력 둘째 줄에 N개의 수 입력 (1,000 이하의 자연수) 출력 입력 받은 수들 중 소수의 개수 출력 풀이 방법 입력 받은 수들의 약수의 개수를 세고, 만약 약수의 개수가 2이면(1과 자기 자신) 소수의 개수를 1만큼 증가 코드 import java.io.*; import java.util.*; public class BOJ_1978 { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N=Integer.parseInt(br.readLine());// 입력 받은 수의 개수 ..
입력 첫 번째 줄에 N의 약수의 개수 입력 둘째 줄에 N의 약수 입력 (1,000,000보다 작거나 같고, 2보다 크거나 같은 자연수, 중복되지 X) 출력 N 출력 풀이 방법 입력 받은 약수를 배열에 저장한 뒤 배열을 오름차순으로 정렬 배열의 첫 번째 값과 마지막 값을 곱하여 N 구하기 코드 import java.io.*; import java.util.*; public class BOJ_1037 { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int num=Integer.parseInt(br.readLine());..
입력 두 개의 자연수 (10,000 이하의 자연수) 출력 첫 줄에는 두 수의 최대공약수 둘째 줄에는 두 수의 최소공배수 풀이방법 유클리드 호제법 이용하여 최대공약수 구하기 GCD(x,y) = GCD(y,r) r=x%y r=0일 때의 y가 최대공약수 최소공배수는 (두 수의 곱)/(최대공약수) 코드 import java.io.*; import java.util.*; public class BOJ_2609 { public static int GCD(int x, int y) { while(y!=0) { int r=x%y; x=y; y=r; } return x; } public static void main(String[] args) throws IOException { BufferedReader br=new B..
입력 n0 (0 < n0 < 1,000,000) 마지막 줄에는 0 하나 입력 출력 케이스 번호를 출력하고, n1이 짝수라면 'even', 홀수라면 'odd'를 출력, n4를 출력 풀이 방법 while문 이용하여 여러 테스트 케이스 반복 cnt 변수 이용하여 테스트 케이스 번호 출력 if문 이용하여 두가지 경우로 나누어 출력 n1이 짝수이면 even 출력 후 n0/2 출력 n1이 홀수이면 odd 출력 후 (n0-1)/2 출력 코드 import java.util.Scanner; public class BOJ_4892 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int cnt=1; while(true) { int n..
입력 integer n : 데이터셋 개수 n개의 줄에 X, Y (X: 좀비가 먹은 뇌의 개수, Y: 좀비가 살기 위해 먹어야 하는 뇌의 개수) 출력 데이터셋 당 한 줄의 출력 (좀비가 먹은 뇌의 개수) ≥ (좀비가 살기위해 먹어야하는 뇌의 개수)라면, “MMM BRAINS” 출력 그 외에는 "NO BRAINS” 출력 풀이 방법 while문을 이용하여 n번 반복해서 X, Y를 입력 받으며 X가 Y보다 크거나 같은 경우에는 “MMM BRAINS”를, 그 외에는 “NO BRAINS”를 출력 코드 import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.StringToken..