728x90
반응형
피자 나눠 먹기 (1)
/**
* 코딩테스트 입문 Day 4 수학, 배열
* 피자 나눠 먹기 (1)
*/
public class Main1 {
public static void main(String[] args) {
System.out.println(solution(7));
System.out.println(solution(1));
System.out.println(solution(15));
}
public static int solution(int n) {
double pizza = (double) n / (double) 7;
return pizza <= 1 ? 1 : (int) Math.ceil(pizza);
}
}
피자 나눠 먹기 (2)
/**
* 코딩테스트 입문 Day 4 수학, 배열
* 피자 나눠 먹기 (2)
*/
public class Main2 {
public static void main(String[] args) {
System.out.println(solution(6));
System.out.println(solution(10));
System.out.println(solution(4));
}
public static int solution(int n) {
int answer = 0;
for (int i = 1; i <= 100; i++) {
if ((6 * i) % n == 0) {
answer = i;
break;
}
}
return answer;
}
}
피자 나눠 먹기 (3)
/**
* 코딩테스트 입문 Day 4 수학, 배열
* 피자 나눠 먹기 (3)
*/
public class Main3 {
public static void main(String[] args) {
System.out.println(solution(7, 10));
System.out.println(solution(4, 12));
}
public static int solution(int slice, int n) {
int answer = 0;
for (int i = 1; i <= 50; i++) {
if (slice * i >= n) {
answer = i;
break;
}
}
return answer;
}
}
배열의 평균값
import java.util.Arrays;
/**
* 코딩테스트 입문 Day 4 수학, 배열
* 배열의 평균값
*/
public class Main4 {
public static void main(String[] args) {
System.out.println(solution(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
System.out.println(solution(new int[]{89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99}));
}
public static double solution(int[] numbers) {
return Arrays.stream(numbers).average().getAsDouble();
}
}
728x90
반응형
'📌Back-End > 프로그래머스' 카테고리의 다른 글
Java 프로그래머스 코딩테스트 입문 Day 6 문자열, 반복문, 출력, 배열, 조건문 (0) | 2024.06.27 |
---|---|
Java 프로그래머스 코딩테스트 입문 Day 5 수학, 배열 (0) | 2024.06.26 |
Java 프로그래머스 코딩테스트 입문 Day 3 사칙연산, 배열, 수학 (0) | 2024.06.24 |
Java 프로그래머스 코딩테스트 입문 Day 2 사칙연산, 조건문, 배열 (0) | 2024.06.23 |
Java 프로그래머스 코딩테스트 입문 Day 1 사칙연산 (0) | 2024.06.22 |