반응형

1. 문제 번호 2562번


 

 

 

2. 문제 풀이 

 - 재귀함수

 - Sorting

 

 

나의 문제풀이 방식 및 순서

 - 처음부터 Sorting만 하면 쉽게 구할텐데 또 재귀함수를 쓰고자 시도함.. 

 

 


3. 소스 인증

 

 

import java.util.*;
import java.lang.*;
import java.io.*;

class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new  InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        List<Integer> numList = new ArrayList<>();

        String inputData = null;
        while( (inputData=br.readLine())!=null ){
            numList.add(Integer.parseInt(inputData));
        }

        int maxValue = maxArray(numList);
        int maxIndex = -1;
        for (int i = 0; i<numList.size(); i++){
            if(numList.get(i) == maxValue){
                maxIndex = numList.indexOf(numList.get(i)) + 1;
                break;
            }
        }
        System.out.println(maxValue);
        System.out.println(maxIndex);
        br.close();
    }

    public static int maxArray(List<Integer> numList){
        if (numList.size() == 1 ){
            return numList.get(0);
        } else {
            return Math.max(numList.get(0), maxArray(numList.subList(1, numList.size()))); 
        }
    }
}

 

 

 

 

 


4. 추가 개념

 

indexOf

String, List계열에서만 사용 가능하며 Index추출이 가능 (단.. Index는 0부터 시작하여 +1 해줘야 할 수도 있음)
Arrays.asList
List 타입으로 변환 

 

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        
    String[] str = {"one","two","three"};
        Arrays.asList(str).indexOf("two"); // 1반환
        
    }
}

 

 

SubList 사용법
특징 1 : 동일한 리스트를 복사하여 만든다.
특징 2 : Main List와 SubList는 연동되어 있어 SubList에 추가하여도 MainList에서도 확인 가능
특징 3 : Main List변경 시 Sub List 사용 불가 
List<Integer> numList = new ArrayList<>();
for(int i = 0; i < 5; i++){
    numList.add(i); //0~4까지 추가
}
List<Integer> sub = numList.subList(3,5); //4,5출력

sub.add(6); //끝에 6추가
sub.remove(0);//0번쨰 요소 삭제

numList.add(7); //원본리스트를 변경하면 subList는 사용불가

 

 

 

 

 

 

 

728x90
반응형

+ Recent posts