본문 바로가기
PS 문제 풀이/Baekjoon

[BOJ] 백준 2566 최댓값

by 까망 하르방 2022. 1. 10.
반응형

Approach

출처https://www.acmicpc.net/problem/2566

9 * 9 숫자를 입력받는데 [행, 열] 위치까지 추적해야 한다.

 

배열로 map[][]로 받아도 되겠지만 memory를 아낀다면

2차원 for문으로 받으면서 값을 비교 갱신해가며 처리해도 된다.

Java

import java.util.Scanner;

public class Main {

	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);

		int nums[][] = new int[10][10];
		int max = 0;
		int x = 0, y = 0;

		for (int i = 0; i < 9; i++) 
		{
			for (int j = 0; j < 9; j++) 
			{
				nums[i][j] = sc.nextInt();
				if (max < nums[i][j]) 
				{
					max = nums[i][j];
					x = i; y = j;
				}
			}
		}

		System.out.println(max);
		System.out.println((x + 1) + " " + (y + 1));
	}
}

 

C++

#include <stdio.h>

int ans, val;
int row, col;

int main()
{
	// freopen("input.txt", "r", stdin);
	row = col = 1;
	for (int x = 1; x <= 9; ++x)
	{
		for (int y = 1; y <= 9; ++y)
		{
			scanf("%d", &val);

			// 최대값이 갱신되는 경우
			if (ans < val)
			{
				ans = val;
				row = x;
				col = y;
			}
		}
	}

	printf("%d\n%d %d\n", ans, row, col);
}
반응형

'PS 문제 풀이 > Baekjoon' 카테고리의 다른 글

[BOJ] 백준 2592 대표값  (0) 2022.01.12
[BOJ] 백준 2588 곱셈  (0) 2022.01.11
[BOJ] 백준 2506 점수계산  (0) 2022.01.08
[BOJ] 백준 2495 연속구간  (0) 2022.01.07
[BOJ] 백준 2490 윷놀이  (0) 2022.01.06

댓글