elesis's haunt

2562 최댓값 본문

백준~문풀 후 최적화 추가~/단계별로 풀어보기

2562 최댓값

elesis 2021. 9. 23. 15:14

* BufferedReader, int[]  두번돌다보니 느리다

import java.io.*;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int sum = 0; int num = 0;
		int[] array = new int[9];
		
		for(int i=0; i<9; i++) {
			array[i] = Integer.parseInt(br.readLine());
			if(array[i]>num) {
				num = array[i];
			}
		}
 	 	br.close();
        
		for(int j=0; j<9; j++) {
			if (num == array[j]) {
				sum = j+1;
			}	
		}
		
		System.out.println(num);
		System.out.println(sum);
	}
}

 

*Scanner, 배열

import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int[] arr = { in.nextInt(), in.nextInt(), in.nextInt(),
				in.nextInt(), in.nextInt(), in.nextInt(),
				in.nextInt(), in.nextInt(), in.nextInt() };
		in.close();
		
		int max = 0; int index = 0; int count = 0;
		
		for(int value : arr) {
			count++;
            
			if(value > max) {
				max = value;
				index = count;
			}
		}
		System.out.print(max + "\n" + index);
	}
}

 

*BufferedReader, 배열

import java.io.*;
 
public class Main {
	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int max = 0; int index = 0; int count = 0;
		int[] arr = new int[9];
        
		for(int i = 0 ; i < 9 ; i++) {
			arr[i] = Integer.parseInt(br.readLine());
		}
 
		for(int value : arr) {
			count++;
			if(value > max) {
				max = value;
				index = count;
			}
		}
		System.out.println(max);
		System.out.println(index);
	}
}

 

*BufferedReader

import java.io.*;
 
public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
		int max = 0; int index = 0;
		for(int i = 0 ; i < 9 ; i++) {
			int val = Integer.parseInt(br.readLine());
			
			if(val > max) {
				max = val;
				index = i+1;
			}
		}
		System.out.println(max);
		System.out.println(index);
	}
}

앞에푼 문제와 같은방식으로 배열없이 가능하다. 이 때 자릿수는 어떻게 가져오나 했는데

더해서 그때 그값을 다른변수에 저장해버리면 되는거였다... 덮이고 덮여서 결국 최대값의 자릿수가 출력될거니까!

'백준~문풀 후 최적화 추가~ > 단계별로 풀어보기' 카테고리의 다른 글

3052 나머지  (0) 2021.09.24
2577 숫자의 개수  (0) 2021.09.23
10818 최소, 최대  (0) 2021.09.16
1110 더하기 사이클  (0) 2021.09.16
10951 A+B - 4  (0) 2021.09.14
Comments