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 |