728x90
반응형
나머지 구하기
/**
* 코딩테스트 입문 Day 3 사칙연산, 배열, 수학
* 나머지 구하기
*/
public class Main1 {
public static void main(String[] args) {
System.out.println(solution(3, 2));
System.out.println(solution(10, 5));
}
public static int solution(int num1, int num2) {
return num1 % num2;
}
}
중앙값 구하기
import java.util.stream.IntStream;
/**
* 코딩테스트 입문 Day 3 사칙연산, 배열, 수학
* 중앙값 구하기
*/
public class Main2 {
public static void main(String[] args) {
System.out.println(solution(new int[]{1, 2, 7, 10, 11}));
System.out.println(solution(new int[]{9, -1, 0}));
}
public static int solution(int[] array) {
int center = array.length / 2;
int[] sortedArr = IntStream.of(array).sorted().toArray();
return sortedArr[center];
}
}
최빈값 구하기
/**
* 코딩테스트 입문 Day 3 사칙연산, 배열, 수학
* 최빈값 구하기
*/
public class Main3 {
public static void main(String[] args) {
System.out.println(solution(new int[]{1, 2, 3, 3, 3, 4}));
System.out.println(solution(new int[]{1, 1, 2, 2}));
System.out.println(solution(new int[]{1}));
}
public static int solution(int[] array) {
int answer = 0;
int max = 0;
int[] frequent = new int[1000];
for (int i = 0; i < array.length; i++) {
frequent[array[i]]++;
if (max < frequent[array[i]]) {
max = frequent[array[i]];
answer = array[i];
}
}
int count = 0;
for (int i = 0; i < 1000; i++) {
if (max == frequent[i]) {
count++;
}
if (count > 1) {
return -1;
}
}
return answer;
}
}
짝수는 싫어요
import java.util.stream.IntStream;
/**
* 코딩테스트 입문 Day 3 사칙연산, 배열, 수학
* 짝수는 싫어요
*/
public class Main4 {
public static void main(String[] args) {
int[] result = solution(10);
for (int num : result) {
System.out.print(num + " ");
}
System.out.println();
result = solution(15);
for (int num : result) {
System.out.print(num + " ");
}
System.out.println();
}
public static int[] solution(int n) {
return IntStream.rangeClosed(1, n).filter(i -> i % 2 != 0).toArray();
}
}
728x90
반응형
'📌Back-End > 프로그래머스' 카테고리의 다른 글
Java 프로그래머스 코딩테스트 입문 Day 5 수학, 배열 (0) | 2024.06.26 |
---|---|
Java 프로그래머스 코딩테스트 입문 Day 4 수학, 배열 (0) | 2024.06.25 |
Java 프로그래머스 코딩테스트 입문 Day 2 사칙연산, 조건문, 배열 (0) | 2024.06.23 |
Java 프로그래머스 코딩테스트 입문 Day 1 사칙연산 (0) | 2024.06.22 |
Java 프로그래머스 코딩 기초 트레이닝 Day 25 이차원 리스트(배열) (0) | 2024.06.21 |