본문 바로가기
study

study - day07

by hj_it 2024. 3. 25.
코딩테스트

[SWEA] java 2단계

 

파리 퇴치 [SWEA 2001]

import java.util.Scanner;
 // N*N 크기의 영역에 각 칸에 파리의 개수가 나와있고,
 // M*M 크기의 파리채를 이용해 최대한 많은 파리를 죽이고자 한다.
 // 죽은 파리의 갯수를 구하라.
class Solution {
    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        int T;
        T=sc.nextInt();
 
        for(int test_case = 1; test_case <= T; test_case++) {
            int N = sc.nextInt();  // 영역의 크기
            int M = sc.nextInt();  // 파리채의 크기
            int total = 0;         // 죽은 파리의 수
            int tmp = 0;           // 임시 파리의 수
            int[][] P = new int[N][N];
                 
            for(int i = 0; i < N; i++) {   // 각 칸의 파리 수 를 입력받음.
                for(int j=0; j < N; j++) {
                    P[i][j] = sc.nextInt();
                }
            }
             for(int i = 0; i < N-M+1; i++) { // M의 값이 최소 2이기 때문에 
                for(int j=0; j < N-M+1; j++) { // N-M+1을 파리채의 시작 위치로 지정
                    tmp = 0;
                    for (int x = 0; x < M; x++) {   // 파리채의 크기만큼 반복
                        for (int y = 0; y < M; y++) {
                            tmp += P[i + x][j + y];
                        }
                    }
                    total = Math.max(tmp, total); //tmp값과 total 갑을 비교하여 큰 값을 반환
                }
             }
            System.out.println("#" + test_case + " " + total);
        }
    }
}

 

초심자의 회문 검사[SWEA 1989]

 

import java.io.*;
import java.util.*;
 // 거꾸로 읽어도 제대로 읽은 것과 같은 문장이나 낱말을 회문이라고 한다.
 // 단어를 입력받아 회문이면 1을, 아니라면 0을 출력하는 프로그램 작성
class Solution {
    public static void main(String args[]) throws Exception {
         
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); //읽는 라인  
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); //출력 라인  
        int T;
        T= Integer.parseInt(bf.readLine());
         
        for(int test_case = 1; test_case <= T; test_case++) {
            String str = bf.readLine();           
            char[] cArr = str.toCharArray();  // 문자열을 문자배열로 변환
            int result = 1;
            int last = cArr.length-1;  // 문자열의 마지막 위치
             
            for (int i = 0; i < cArr.length/2; i++) { // 문자열의 중간 값까지 반복
                if (cArr[i] == cArr[last]) { //일치할 경우 
                    last--;  // 마지막 위치 값을 1씩 빼줌.
                    continue;
                }
                else {   // 불일치
                    result = 0;
                    break;  // for문을 나옴.
                }          
            }
            bw.write(String.valueOf("#" + test_case + " " + result));
            bw.newLine();
        }
         bw.flush();
         bw.close();
    }
}

'study' 카테고리의 다른 글

study - day10  (0) 2024.04.01
study - day09  (0) 2024.04.01
study - day08  (0) 2024.04.01
study - day02  (0) 2024.03.13
study - day01  (0) 2024.03.12