📌Back-End/프로그래머스
Java 프로그래머스 코딩 기초 트레이닝 Day 21 함수(메서드)
구 일
2024. 6. 17. 22:10
728x90
반응형
뒤에서 5등 위로
import java.util.Arrays;
/**
* 코딩 기초 트레이닝 Day 21
* 뒤에서 5등 위로
*/
public class Main1 {
public static void main(String[] args) {
int[] result = solution(new int[]{12, 4, 15, 46, 38, 1, 14, 56, 32, 10});
for (int num : result) {
System.out.print(num + " ");
}
System.out.println();
}
public static int[] solution(int[] num_list) {
Arrays.sort(num_list);
return Arrays.copyOfRange(num_list, 5, num_list.length);
}
}
전국 대회 선발 고사
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
/**
* 코딩 기초 트레이닝 Day 21
* 전국 대회 선발 고사
*/
public class Main2 {
public static void main(String[] args) {
System.out.println(solution(new int[]{3, 7, 2, 5, 4, 6, 1}, new boolean[]{false, true, true, true, true, false, false}));
System.out.println(solution(new int[]{1, 2, 3}, new boolean[]{true, true, true}));
System.out.println(solution(new int[]{6, 1, 5, 2, 3, 4}, new boolean[]{true, false, true, false, false, true}));
}
public static int solution(int[] rank, boolean[] attendance) {
HashMap<Integer, Integer> hm = new HashMap<>();
for (int i = 0; i < rank.length; i++) {
if (attendance[i]) {
hm.put(rank[i], i);
}
}
LinkedList<Integer> list = new LinkedList<>(hm.keySet());
list.sort(Comparator.naturalOrder());
int a = hm.get(list.get(0));
int b = hm.get(list.get(1));
int c = hm.get(list.get(2));
return 10000 * a + 100 * b + c;
}
}
정수 부분
/**
* 코딩 기초 트레이닝 Day 21
* 정수 부분
*/
public class Main3 {
public static void main(String[] args) {
System.out.println(solution(1.42));
System.out.println(solution(69.32));
}
public static int solution(double flo) {
return (int) flo;
}
}
문자열 정수의 합
/**
* 코딩 기초 트레이닝 Day 21
* 문자열 정수의 합
*/
public class Main4 {
public static void main(String[] args) {
System.out.println(solution("123456789"));
System.out.println(solution("1000000"));
}
public static int solution(String num_str) {
int answer = 0;
String[] strArr = num_str.split("");
for (String s : strArr) {
answer += Integer.parseInt(s);
}
return answer;
}
}
문자열을 정수로 변환하기
/**
* 코딩 기초 트레이닝 Day 21
* 문자열을 정수로 변환하기
*/
public class Main5 {
public static void main(String[] args) {
System.out.println(solution("10"));
System.out.println(solution("8542"));
}
public static int solution(String n_str) {
return Integer.parseInt(n_str);
}
}
728x90
반응형