본문 바로가기
온라인 저지/ETC.

[JOL/Java] 1037 오류교정

by ahj 2022. 2. 20.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class JOL1037 {

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int[][] map = new int[n + 1][n + 1];

		StringTokenizer st;
		int[] cntR = new int[n + 1];
		int[] cntC = new int[n + 1];
		int rOddIndex = 0;
		int cOddIndex = 0;
		boolean failure = false;
		for (int i = 1; i <= n; i++) {
			st = new StringTokenizer(br.readLine());
			for (int j = 1; j <= n; j++) {
				int tmp = Integer.parseInt(st.nextToken());
				map[i][j] = tmp;
				cntR[i] += tmp;
			}
			// 행 홀수 index 체크
			if (cntR[i] % 2 == 1) {
				if (rOddIndex != 0) {
					// 1. 어디든 홀수가 2개 이상이면 실패
					failure = true;
					continue;
				}
				rOddIndex = i;
			}
		}
		if (!failure) {
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= n; j++) {
					cntC[i] += map[j][i];
				}
				if (cntC[i] % 2 == 1) {
					if (cOddIndex != 0) {
						// 1. 어디든 홀수가 2개 이상이면 실패
						failure = true;
						break;
					}
					cOddIndex = i;
				}
			}
		}
		if (failure) {
			System.out.println("Corrupt");
		} else {
			// 3. 홀수가 없으면 OK
			if (cOddIndex == 0 && rOddIndex == 0) {
				System.out.println("OK");
			} else {
				// 2. 홀수가 1개씩만 있다면 change
				System.out.println(String.format("Change bit (%d, %d)", rOddIndex, cOddIndex));
			}
		}
	}

}

행의 경우는 입력과 동시에 처리해줬다. 기억해놔야할 값들은 다 변수를 만들어서 처리해줬다. 

'온라인 저지 > ETC.' 카테고리의 다른 글

[JOL/Java] 1828 냉장고  (0) 2022.02.15
[CodeUp/C] 성실한 개미  (0) 2021.09.22
[코드업 100제/python] 96제  (0) 2021.08.16

댓글