📌Back-End/프로그래머스
Java 프로그래머스 코딩테스트 입문 Day 23 배열, 정렬, 문자열
구 일
2024. 7. 17. 17:51
728x90
반응형
특이한 정렬
import java.util.Arrays;
/**
* 코딩테스트 입문 Day 23 배열, 정렬, 문자열
* 특이한 정렬
*/
public class Main1 {
public static void main(String[] args) {
int[] result = solution(new int[]{1, 2, 3, 4, 5, 6}, 4);
for (int num : result) {
System.out.print(num + " ");
}
System.out.println();
result = solution(new int[]{10000, 20, 36, 47, 40, 6, 10, 7000}, 30);
for (int num : result) {
System.out.print(num + " ");
}
System.out.println();
}
public static int[] solution(int[] numlist, int n) {
Arrays.sort(numlist);
for (int i = 0; i < numlist.length; i++) {
for (int j = 0; j < numlist.length; j++) {
if (Math.abs(numlist[i] - n) <= Math.abs(numlist[j] - n)) {
int tmp = numlist[i];
numlist[i] = numlist[j];
numlist[j] = tmp;
}
}
}
return numlist;
}
}
등수 매기기
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* 코딩테스트 입문 Day 23 배열, 정렬, 문자열
* 등수 매기기
*/
public class Main2 {
public static void main(String[] args) {
int[] result = solution(new int[][]{{80, 70}, {90, 50}, {40, 70}, {50, 80}});
for (int num : result) {
System.out.print(num + " ");
}
System.out.println();
result = solution(new int[][]{{80, 70}, {70, 80}, {30, 50}, {90, 100}, {100, 90}, {100, 100}, {10, 30}});
for (int num : result) {
System.out.print(num + " ");
}
System.out.println();
}
public static int[] solution(int[][] score) {
List<Integer> list = new ArrayList<>();
for (int[] a : score) {
list.add(a[0] + a[1]);
}
list.sort(Comparator.reverseOrder());
int[] answer = new int[score.length];
for (int i = 0; i < score.length; i++) {
answer[i] = list.indexOf(score[i][0] + score[i][1]) + 1;
}
return answer;
}
}
옹알이 (1)
/**
* 코딩테스트 입문 Day 23 배열, 정렬, 문자열
* 옹알이 (1)
*/
public class Main3 {
public static void main(String[] args) {
System.out.println(solution(new String[]{"aya", "yee", "u", "maa", "wyeoo"}));
System.out.println(solution(new String[]{"ayaye", "uuuma", "ye", "yemawoo", "ayaa"}));
}
public static int solution(String[] babbling) {
int answer = 0;
String[] arr = {"aya", "ye", "woo", "ma"};
for (int i = 0; i < babbling.length; i++) {
for (String s : arr) {
babbling[i] = babbling[i].replace(s, " ");
}
if (babbling[i].trim().isEmpty()) {
answer++;
}
}
return answer;
}
}
로그인 성공?
/**
* 코딩테스트 입문 Day 23 배열, 정렬, 문자열
* 로그인 성공?
*/
public class Main4 {
public static void main(String[] args) {
System.out.println(solution(
new String[]{"meosseugi", "1234"},
new String[][]{{"rardss", "123"}, {"yyoom", "1234"}, {"meosseugi", "1234"}}
));
System.out.println(solution(
new String[]{"programmer01", "15789"},
new String[][]{{"programmer02", "111111"}, {"programmer00", "134"}, {"programmer01", "1145"}}
));
System.out.println(solution(
new String[]{"rabbit04", "98761"},
new String[][]{{"jaja11", "98761"}, {"krong0313", "29440"}, {"rabbit00", "111333"}}
));
}
public static String solution(String[] id_pw, String[][] db) {
String id = id_pw[0];
String pw = id_pw[1];
for (int i = 0; i < db.length; i++) {
if (db[i][0].equals(id)) {
if (db[i][1].equals(pw)) {
return "login";
} else {
return "wrong pw";
}
}
}
return "fail";
}
}
728x90
반응형