반응형
{ answer : 정답 번호 (1~5), score : number }로 이루어진 배열 arr이 정답지로 넘어갈 때,
가장 점수가 높은 수포자의 이름과 점수를 “이름 : 점수” 형태로 리턴해 주세요
시험 응시자는 1번으로 모두 찍은 a, 3번으로 모두 찍은 b, 5번으로 모두 찍은 c가 있습니다.
[입출력 예]
[ { answer : 1, score : 5 }, { answer : 3, score : 3 }, { answer : 2, score : 4 } ] → a : 5
[ { answer : 1, score : 5 }, { answer : 5, score : 5 }, { answer : 2, score : 4 } ] → a : 5, c : 5
*이클립스에서 돌려보며 정답 맞춰본 코드
import java.util.Arrays;
public class main {
public static void main(String args[]) {
int[] test = new int[]{1,2,4,1,1};
solution3();
}
private static void solution3() {
//int[] answer = new int[]{1,2,4,1,1};
int[] answer = {};
int[] a = new int[] {1, 1, 1, 1, 1};
int[] b = new int[] {3, 3, 3, 3, 3};
int[] c = new int[] {5, 5, 5, 5, 5};
int[] score = new int[3];
//score[]에 맞힌 문제 채점
for(int i=0; i<answer.length; i++) {
if(answer[i] == a[i%answer.length]) {score[0]++;}
if(answer[i] == b[i%answer.length]) {score[1]++;}
if(answer[i] == c[i%answer.length]) {score[2]++;}
}
//높은 점수
int idx=0;
int max = score[0];
for(int i=1; i<score.length; i++)
{
if(max < score[i]) {
idx = i;
max = score[i];
}
}
String str = "A";
if(idx== 1) str= "B";
if(idx==2) str = "C";
System.out.println( str + " : " + max );
}
}
반응형
'개발 기록 > backend' 카테고리의 다른 글
[백엔드] 프리온보딩 객체지향 1차시 - 소프트웨어 가치에서 시작하는 객체지향 (2) | 2024.01.07 |
---|---|
[백엔드]대규모 설계 시스템 기초_사전미션 (1) | 2023.12.29 |
[백엔드] 원티드- Mysql 기본기 다지기_사전미션 (0) | 2023.12.29 |
[백엔드]원티드 - 프로젝트 유연함을 높이는 객체지향 첫걸음_사전미션 (0) | 2023.12.27 |
[Java] 소수의 합 (1) | 2023.12.06 |