코딩테스트/백준(JAVA)

백준 코딩테스트 4-4-2562 최대값 코딩 문제 java로 푸는 방법

쏠솔랄라 2023. 3. 29. 09:21

 

 

백준 코딩테스트 단계별로 풀어보기

4단계 1차원 배열 4단계 2562 최대값 문제입니다

 

 

https://www.acmicpc.net/problem/2562

 

2562번: 최댓값

9개의 서로 다른 자연수가 주어질 때, 이들 중 최댓값을 찾고 그 최댓값이 몇 번째 수인지를 구하는 프로그램을 작성하시오. 예를 들어, 서로 다른 9개의 자연수 3, 29, 38, 12, 57, 74, 40, 85, 61 이 주어

www.acmicpc.net

 

 

https://www.acmicpc.net/step

 

단계별로 풀어보기

단계별은 @jh05013님이 관리하고 계십니다. 단계제목설명정보총 문제내가 맞은 문제1입출력과 사칙연산입력, 출력과 사칙연산을 연습해 봅시다. Hello World!132조건문if 등의 조건문을 사용해 봅시다

www.acmicpc.net

 

 


 

 

 

문제
9개의 서로 다른 자연수가 주어질 때,
이들 중 최댓값을 찾고 그 최댓값이 몇 번째 수인지를 구하는 프로그램을 작성하시오.
예를 들어,
서로 다른 9개의 자연수3, 29, 38, 12, 57, 74, 40, 85, 61이 주어지면,
이들 중 최댓값은 85이고, 이 값은 8번째 수이다.

입력
첫째 줄부터 아홉 번째 줄까지 한 줄에 하나의 자연수가 주어진다.
주어지는 자연수는 100 보다 작다.

출력
첫째 줄에 최댓값을 출력하고, 둘째 줄에 최댓값이 몇 번째 수인지를 출력한다.

 

 

정답

import java.util.Scanner;

import java.util.Arrays;

public class Main {

 

public static void main(String[] args) {

 

Scanner sc = new Scanner(System.in);

 

int[] arr = new int[9];

 

for (int i=0; i<9; i++) {

int n=sc.nextInt();

arr[i] = n;

}

 

int max=0;

int index=0;

 

for (int i=0; i<9; i++) {

if (arr[i]>max) {

max = arr[i];

index = i+1;

}

}

System.out.println(max);

System.out.println(index);

}

}

 

=>

가장 익숙한 풀이

9개 숫자 스캐너로 받아서 배열에 넣고

for문으로 돌려가면서 가장 큰 수와 인덱스 구하기

 

 

<또다른 풀이>

import java.util.Scanner;

public class Main. {

public static void main(String[] args) {

 

Scanner sc = new Scanner(System.in);

 

int[] arr = { sc.nextInt(), sc.nextInt(), sc.nextInt(),

sc.nextInt(), sc.nextInt(), sc.nextInt(),

sc.nextInt(), sc.nextInt(), sc.nextInt()};

 

int cnt = 0;

int max = 0;

int index = 0;

 

for(int val : arr) {

cnt++;

 

if(val > max) {

max = val;

index = cnt;

}

}

System.out.print(max + "\n" + index);

}

}

 

=>

또다른 scanner 풀이

지금처럼 입력받는 수가 9개로 명확히 정해진 경우

array안에 들어갈 수 9개를 바로 입력해주면

소요하는 메모리가 달라지지 않을까 해서 풀어봄

메모리 차이는 거의 없었다

 

 

<또다른 풀이>

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Main {

public static void main(String[] args) throws IOException {

 

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\

int[] arr = new int[9];

 

for (int i=0; i<9; i++) {

arr[i] = Integer.parseInt(br.readLine());

}

 

int max=0;

int index=0;

 

for (int i=0; i<9; i++) {

if (arr[i]>max) {

max = arr[i];

index = i+1;

}

}

System.out.println(max);

System.out.println(index);

}

}

 

=> 

스캐너 풀이와 동일하고

입력만 bufferedreader로 받음

 

  

맨 위부터 bufferedreader, 중간이 첫번째 풀이, 가장 마지막이 두번째 풀이

동일하게 scanner로 받으면 직접 입력받나 for문으로 입력받나

메모리에서 유의미한 차이는 없음

bufferedreader가 유의미한 차이를 보인다

 

 


 

 

1-1-2557 Hello World https://developernew.tistory.com/41 <- 백준 답지 제출 방법
1-2-1000 A+B https://developernew.tistory.com/42
1-3-1001 A-B https://developernew.tistory.com/43
1-4-10998 AXB https://developernew.tistory.com/44
1-5-1008 A/B https://developernew.tistory.com/45
1-6-10869 사칙연산 https://developernew.tistory.com/46
1-7-10926 ??! https://developernew.tistory.com/47
1-8-18108 1998년생인 내가 태국에서는 2541년생?! https://developernew.tistory.com/48
1-9-10430 나머지 https://developernew.tistory.com/49
1-10-2588 곱셈 https://developernew.tistory.com/50
1-11-11382 꼬마 정민 https://developernew.tistory.com/51
1-12-10171 고양이 https://developernew.tistory.com/52
1-13-10172 개 https://developernew.tistory.com/53

2-1-1330 두 수 비교하기 https://developernew.tistory.com/54
2-2-9498 시험 성적 https://developernew.tistory.com/55
2-3-2753 윤년 https://developernew.tistory.com/56
2-4-14681 사분면 고르기 https://developernew.tistory.com/61
2-5-2884 알람 시계 https://developernew.tistory.com/68
2-6-2525 오븐 시계 https://developernew.tistory.com/69
2-7-2480 주사위 https://developernew.tistory.com/72

3-1-2739 구구단 https://developernew.tistory.com/73
3-2-10950 A+B - 3 https://developernew.tistory.com/75
3-3-8393 합 https://developernew.tistory.com/77
3-4-25304 영수증 https://developernew.tistory.com/79
3-5-25314 코딩은 체육과목 입니다 https://developernew.tistory.com/82
3-6-15552 빠른 A+B https://developernew.tistory.com/83
3-7-11021 A+B -7 https://developernew.tistory.com/85
3-8-11022 A+B -8 https://developernew.tistory.com/86
3-9-2438 별 찍기 -1 https://developernew.tistory.com/87
3-10-2439 별 찍기 -2 https://developernew.tistory.com/89
3-11-10952 A+B -5 https://developernew.tistory.com/90
3-12-10951 A+B -4 https://developernew.tistory.com/91

4-1-10807 개수 세기 https://developernew.tistory.com/97
4-2-10871 X보다 작은 수 https://developernew.tistory.com/100
4-3-10818 최소, 최대 https://developernew.tistory.com/101