지-코바

[지코바] 12/10 문제풀이

moonseok 2021. 12. 10. 01:50

1.체스판 다시 칠하기

package jicoba;

import java.util.Scanner;

public class chessAgain {
	public static int squaresToRewrite =64;
	public static boolean[][] chessBoard;
	
	public static void findSquaresToRewrite(int x, int y) {
		int lastX = x + 8;
		int lastY = y + 8;

		int wrongSquares = 0;

		boolean FirstBoolean = chessBoard[x][y];

		for (int i = x; i < lastX; i++) {
			for (int j = y; j < lastY; j++) {

				if (chessBoard[i][j] != FirstBoolean) {
					wrongSquares++;
				}
				FirstBoolean = (!FirstBoolean);
			}

			FirstBoolean = !FirstBoolean;

		}
		
		wrongSquares = Math.min(wrongSquares, 64 - wrongSquares);
		squaresToRewrite = Math.min(squaresToRewrite, wrongSquares);

	}

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int M;
		int N;
		M = sc.nextInt();
		N = sc.nextInt();
		
		chessBoard = new boolean[M][N];
		sc.nextLine();
		for(int i = 0; i < N; i++) {
			String input = sc.nextLine();
			for(int j = 0; j < M; j++) {
				if(input.charAt(j) == 'W') {
					chessBoard[i][j] = true;
				}else{
					chessBoard[i][j] = false;
				}
			}
		}
		
		int N_row = M - 7;
		int M_col = N - 7;

		for (int i = 0; i < N_row; i++) {
			for (int j = 0; j < M_col; j++) {
				findSquaresToRewrite(i, j);
			}
		}
		
		System.out.println(squaresToRewrite);
	}
}

2.직사각형에서 탈출

package jicoba;

import java.util.Arrays;
import java.util.Scanner;

public class shortestWay {
	public static void main(String[] args) {
		int x;
		int y;
		int w;
		int h;

		Scanner sc = new Scanner(System.in);
		x = sc.nextInt();
		y = sc.nextInt();
		w = sc.nextInt();
		h = sc.nextInt();
		
		int array[] = {w-x, x, h-y, y};
		
		Arrays.sort(array);
		int min = array[0];
		System.out.println(min);
		
		
	}

}

3. 단어정렬

package jicoba;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class sortWord {
	
	static void arraySorting(String[] arr) {
		
		Arrays.sort(arr, new Comparator<String>() {
			public int compare(String word1, String word2) {
				if (word1.length() == word2.length()) {
					return word1.compareTo(word2);
				} 
				else {
					return word1.length() - word2.length();
				}
			  }
		  }
	   );
		
	}
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int howMany = sc.nextInt();
		String[] wordToSort = new String[howMany];
		
		sc.nextLine();
		
		for(int i = 0; i < howMany; i++) {
			wordToSort[i] = sc.nextLine();
		}
		
		arraySorting(wordToSort);
		
		
		StringBuilder result = new StringBuilder();
		 
		result.append(wordToSort[0] + "\n");
		
		for (int i = 1; i < howMany; i++) {
			if (!wordToSort[i].equals(wordToSort[i-1])) {
				result.append(wordToSort[i] + "\n");
			}
		}
		System.out.println(result);
		
	}

}

4.팰린드롬수

package jicoba;

import java.util.Scanner;

public class palin {
	
	public static String checkPalindrome(String str) {
		
		String result = null ;
		int end = str.length() - 1;
		for(int i = 0; i < str.length()/2; i++,end--) {
			if(str.charAt(i) == str.charAt(end)) {
				result =  "yes";
			}else {
				result =  "no";
			}
		}
		return result;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String pal = sc.next();		
		System.out.println(checkPalindrome(pal));
		
	}
	
}

'지-코바' 카테고리의 다른 글

[지코바] 3회차 문제풀이  (0) 2022.02.03
[지코바] 2회차 문제풀이  (0) 2022.01.14
[지코바]5회차 문제풀이 - 상속  (0) 2021.11.25
4회차 문제풀이 - 논리연산자  (0) 2021.10.28
지코바 4회차 - 반복문  (0) 2021.10.15