Algorithm(23.1.23)

2023. 1. 23. 16:46·algorithm

10818번

 

방법 1)

최대 최소를 구하는 알고리즘을 구현

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int A[]=new int[N+1];
        int x=0;
        int max=0;
        int min=0;

        for(int i=0; i<N; i++){
            A[i] = sc.nextInt();
        } //배열에 값 넣기
        A[5] = 10;

        for(int j=0; j<N; j++){
            if(A[j+1]>A[j]){
                max = A[j];
            }
            else if(A[j+1]<A[j]){
                min = A[j];
            }
        }
        System.out.println(min +" "+ max);
    }
}

어째서인지 오류가 나왔고 결국 더 쉬운 방법이 있었다.

 

방법 2)

Array 사용

import java.util.Arrays;
import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int A[]=new int[N];

        for(int i=0; i<A.length; i++){
            A[i] = sc.nextInt();
        } //배열에 값 넣기

        Arrays.sort(A); //Array.sort 메소드!

        System.out.println(A[0]+" "+A[N-1]);  //배열의 크기와 인덱스는 다르다는 것 중요.!
    }
}

Array기능을 사용하니 알고리즘을 따로 구현할 필요가 없어서 매우 편리했다.

저작자표시 (새창열림)

'algorithm' 카테고리의 다른 글

Algorithm(23.2.2)  (0) 2023.02.02
Algorithm(23.1.29)  (0) 2023.01.31
Algorithm(23.1.19)  (0) 2023.01.19
Algorithm (23.1.5)  (1) 2023.01.07
'algorithm' 카테고리의 다른 글
  • Algorithm(23.2.2)
  • Algorithm(23.1.29)
  • Algorithm(23.1.19)
  • Algorithm (23.1.5)
noeyh
noeyh
기록하고 성장하는 개발자, 최유현 입니다. github : https://github.com/Choiyuhyeon
    티스토리 홈 로그아웃
  • noeyh
    CreateU
    noeyh
  • 전체
    오늘
    어제
  • 글쓰기 관리
    GitHub
    Notion
    • 분류 전체보기 (31)
      • web (22)
        • html (5)
        • css (4)
        • js (5)
        • react (6)
        • next (1)
      • server (0)
      • linux (0)
      • figma (0)
      • ml (0)
      • algorithm (5)
      • git (1)
      • 미니프로젝트 (0)
      • 프로젝트 (0)
      • 정보 (0)
      • 일상 (0)
      • memo (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • hELLO· Designed By정상우.v4.10.6
noeyh
Algorithm(23.1.23)
상단으로

티스토리툴바