-
[백준] 2562번 최댓값[ DEV ] Backend/[백준] 브론즈 마스터하기 2022. 7. 17. 20:39
https://www.acmicpc.net/problem/2562
2562번: 최댓값
9개의 서로 다른 자연수가 주어질 때, 이들 중 최댓값을 찾고 그 최댓값이 몇 번째 수인지를 구하는 프로그램을 작성하시오. 예를 들어, 서로 다른 9개의 자연수 3, 29, 38, 12, 57, 74, 40, 85, 61 이 주어
www.acmicpc.net
💻Scanner 를 이용한 문제풀이
배열의 원소 갯수는 처음부터 정해져 있어서 9로 선언해주었다.
최댓값과 몇번째인지 세어주는 카운트 함수를 설정하고 for문을 활용해 값을 구해보았다.
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int arr[] = new int[9]; int max = arr[0]; int count = 0; for (int i = 0; i < 9; i++) { int num = sc.nextInt(); arr[i] = num; if(arr[i] >= max ){ max = arr[i]; count=i+1; } } System.out.println(max); System.out.println(count); } }
💻BufferedReader 와 BufferedWriter 를 활용한 문제풀이
풀긴 풀었는데, 거의 다른 사람들 코드 따라서 제출한 찝찝한 기분이 든다.
특히 for-each 문을 사용하는데에 있어서 자유롭지 못해 따로 찾아보고 정리를 해뒀다.
https://ssoontory.tistory.com/45
for문, for each 문
for문 빠져나가기 (break) for(int i = 0; i< money ; i++){ if (money == 110){ System.out.println("장사 끝!!"); break; } money += 20; coffee --; System.out.println("돈을 받았으니 커피를 판매합니다. 현..
ssoontory.tistory.com
import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int[] arr = new int[9]; for (int i = 0; i < 9 ; i++) { arr[i] = Integer.parseInt(br.readLine()); } int index = 0; int count = 0; int max = 0; for(int value : arr){ count++; if(value>=max){ max = value; index = count; } } bw.write(max + "\n" + index); bw.flush(); } }
'[ DEV ] Backend > [백준] 브론즈 마스터하기' 카테고리의 다른 글
[백준] 1110번 _더하기 사이클 (자바) BufferedReader 사용 (0) 2022.07.17 [백준] 15552번 _빠른 A+B (자바) (0) 2022.07.17 [백준] 10818번 최소, 최대 (0) 2022.07.17 [백준] 2577번 _숫자의 개수 (자바) (0) 2022.07.14 [백준] 10871번 _ X보다 작은 수 (자바) (0) 2022.07.04