Algorithm(23.1.29)

2023. 1. 31. 20:45·algorithm

2562

방법을 두가지를 이용해보았다.

 

방법1)

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int A[]=new int[9];
        int max = 0;
        int index = 0;

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

        for(int j=0; j<9; j++) { //어려움을 겪었던 부분
            if (A[j] > max) {
                max = A[j];
                index = j+1;
            }
        }

        System.out.println(max);  //배열의 크기와 인덱스는 다르다는 것 중요.!
        System.out.println(index);
    }
}

어려움을 겪었던 부분을 또 다르게 작성하는 방법도 있다.

for (int j = 1; j < 9; j++) {
    if (A[j] > A[j - 1]) {
        max = A[j];
        index = j;
    }
}

배열을 사용할 때 인덱스의 크기와 자리는 다르다는 것을 항상 인지해야 한다.


 

저작자표시 (새창열림)

'algorithm' 카테고리의 다른 글

Algorithm(23.2.2)  (0) 2023.02.02
Algorithm(23.1.23)  (0) 2023.01.23
Algorithm(23.1.19)  (0) 2023.01.19
Algorithm (23.1.5)  (1) 2023.01.07
'algorithm' 카테고리의 다른 글
  • Algorithm(23.2.2)
  • Algorithm(23.1.23)
  • 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.29)
상단으로

티스토리툴바