java/클래스와 메서드
옷 사이즈
june__Park
2021. 3. 25. 00:17
package week5;
import java.util.Arrays;
public class Test1 {
/*
* ##### 예시 설명 "XS"와 "XL"은 각각 한명씩 신청했습니다. "S"와 "L"은 각각 두 명씩 신청했습니다. "M"과 "XXL"을
* 신청한 학생은 없습니다.
*/
public int[] solution(String[] shirtSize) {
int[] answer = new int[6];
for (int i = 0; i < shirtSize.length; i++) {
if (shirtSize[i] == "XS") {
answer[0] += 1;
} else if (shirtSize[i] == "S") {
answer[1] += 1;
} else if (shirtSize[i] == "M") {
answer[2] += 1;
} else if (shirtSize[i] == "L") {
answer[3] += 1;
} else if (shirtSize[i] == "XL") {
answer[4] += 1;
} else if (shirtSize[i] == "XXL") {
answer[5] += 1;
}
}
return answer;
}
public static void main(String[] args) {
Test1 sol = new Test1();
String[] shirtSize = { "XS", "S", "L", "L", "XL", "S" };
int[] ret = sol.solution(shirtSize);
System.out.println("답: " + Arrays.toString(ret) + " .");
String[] sizeInfo = { "XS", "S", "M", "L", "XL", "XXL" };
int[] count = ret;
int cnt = 0;
for (int i = 0; i < count.length; i++) {
if (count[i] != 0) {
cnt++;
}
}
for (int i = 0; i < count.length; i++) {
int maxCount = count[i];
int maxIdx = i;
for (int j = i; j < count.length; j++) {
if (maxCount < count[j]) {
maxCount = count[j];
maxIdx = j;
}
}
int countTemp = count[i];
count[i] = count[maxIdx];
count[maxIdx] = countTemp;
String nameTemp = sizeInfo[i];
sizeInfo[i] = sizeInfo[maxIdx];
sizeInfo[maxIdx] = nameTemp;
}
for (int i = 0; i < cnt; i++) {
System.out.println(sizeInfo[i] + " : " + count[i]);
}
}
}