/* 가장 큰 수 찾기
* 정수 배열 array가 매개변수로 주어질 때,
* 가장 큰 수와 그 수의 인덱스를 담은 배열을 return 하도록
*
* array result
* [1, 8, 3] [8, 1] 1, 8, 3 중 가장 큰 수는 8이고 인덱스 1에
* [9, 10, 11, 8] [11, 2] 1, 8, 3 중 가장 큰 수는 8이고 인덱스 1에
*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public class programmer_0_18 { static int[] a1 = {1,8,3}; static int[] a2 = {9,10,11,8}; public int[] solution(int[] array) { int[] answer = new int[2]; for(int i = 0; i < array.length; i++){ if(i == 0){ answer[0] = array[i]; answer[1] = i; } else{ if(answer[0] < array[i]){ answer[0] = array[i]; answer[1] = i; } } } return answer; } public static void main(String args[]){ programmer_0_18 t = new programmer_0_18(); System.out.println("---------------------------------------"); System.out.println("result = " + t.solution(a1)); System.out.println("---------------------------------------"); System.out.println("result = " + t.solution(a2)); System.out.println("---------------------------------------"); } } | cs |