리우's IT Story
article thumbnail
728x90
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/120847

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

 

 

class Solution {
    public int solution(int[] numbers) {
        int answer = 0;
        int max=0;
        int index = 0;
        for(int i =0; i<numbers.length-1; i++){
            if(max < numbers[i]) {
                max = numbers[i];
                index = i;
            }
            
        }
        for(int i =0; i<numbers.length; i++){
            if(i!=index && answer < max * numbers[i]){
                answer = max * numbers[i];
            }
        }
        return answer;
    }
}

 

 

answer =  리턴할 변수 
max = 최댓값을 지정할 변수
index = 요소를 저장할 변수 

정수형 배열 numbers의 길이만큼 루프시키면서
max  numbers[i]보다 작으면 
max에 numbers[i] 값을 지정하고  index 값에 i를 넣는다. 

numbers의 길이만큼 반복시키면서
index 가 i가 아니면서  numbers의 i의 요소가 제일큰 수와   다른 i요소를 곱한다. 
answer 에 담아준다. 

 

728x90
반응형
profile

리우's IT Story

@LRWoo

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!