https://www.acmicpc.net/problem/2562
여러줄을 입력받아야하는 문제이다.
생각보다 어렵진 않았는데 index위치를 찾는 데에서 약간 고민했다.
특정 인덱스의 위치를 찾는 메소드인 indexOf를 써봤는데 이상한 값이 출력되길래
i의 값을 출력했다.
package 백준;// @ author ninaaano
import java.util.Scanner;
public class b_2562 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = new int[9];
int max = arr[0]; // 최댓값을 담을 변수
int index = 0; // 최댓값의 위치를 담는 변수
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
if (max < arr[i]) {
max = arr[i];
index = i+1;
}
}
System.out.println(max);
System.out.println(index);
}
}
BufferedReader를 사용할까하다가 간단하게 Scanner를 사용했다.
나중에 코테 풀려면 버퍼에 담는게 시간 단축에 도움이 되니까 한번 구현해봤다.
while문에 담아야 하나 했는데 for문안에서 입력을 받았다.
여러줄 입력이 되는구나....?
package 백준;// @ author ninaaano
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class b_2562 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int arr[] = new int[9];
int max = arr[0]; // 최댓값을 담을 변수
int index = 0; // 최댓값의 위치를 담는 변수
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(br.readLine());
if (max < arr[i]) {
max = arr[i];
index = i+1;
}
}
System.out.println(max);
System.out.println(index);
}
}
확실히 시간이 많이 단축된다.