CT

[BOJ] 6단계 심화

kinggora 2023. 4. 3. 00:55
3 2444 별 찍기 - 7

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int length = 2 * n - 1;
        String[] result = new String[length];
        //4 1 / 3 3 /2 5 / 1 7 / 0 9 (2 x n - 1)
        for(int i = 1; i <= n; i++) {
            StringBuilder sb = new StringBuilder();
            for(int j = 0; j < n - i; j++) {
                sb.append(" ");
            }
            for(int k = 0; k < 2 * i - 1; k++) {
                sb.append("*");
            }
            result[i-1] = sb.toString();
            if(i != n) {
                result[length-i] = result[i-1];
            }
        }
        for(String s: result) {
            System.out.println(s);
        }
    }
}

 

4 10812 바구니 순서 바꾸기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
        int N = Integer.parseInt(tokenizer.nextToken());
        int M = Integer.parseInt(tokenizer.nextToken());

        List<Integer> bucket = new ArrayList<>();
        for(int i = 0; i < N; i++) {
            bucket.add(i+1);
        }

        for(int i = 0; i < M; i++) {
            tokenizer = new StringTokenizer(reader.readLine());
            int start = Integer.parseInt(tokenizer.nextToken()) - 1;
            int end = Integer.parseInt(tokenizer.nextToken()) - 1;
            int mid = Integer.parseInt(tokenizer.nextToken()) - 1;

            for(int j = 0; j < end - mid + 1; j++) {
                int val = bucket.remove(mid + j);
                bucket.add(start + j, val);
            }

        }

        StringBuilder sb = new StringBuilder();
        for(int num : bucket) {
            sb.append(num).append(" ");
        }
        System.out.print(sb);
    }
}

 

5 10988 팰린드롬인지 확인하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String str = reader.readLine();
        int length = str.length();
        int result = 1;
        for(int i = 0; i < length/2; i++) {
            if(str.charAt(i) != str.charAt(length-1-i)){
                result = 0;
                break;
            }
        }
        System.out.println(result);
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String original = reader.readLine();
        StringBuilder sb = new StringBuilder(original);
        String reverse = sb.reverse().toString();
        if(original.equals(reverse)){
            System.out.println(1);
        } else {
            System.out.println(0);
        }

    }
}

*StringBuilder.reverse() 사용

 

6 1157 단어 공부
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        char[] str = reader.readLine().toCharArray();
        int[] cntArr = new int[26];

        //알파벳당 사용 횟수 카운트
        for(char c : str) {
            if('A' <= c && c <= 'Z') { //대문자인 경우
                cntArr[c - 'A']++;
            } else if('a' <= c && c <= 'z') { //소문자인 경우
                cntArr[c - 'a']++;

            }
        }

        //최대값 구하기
        int max = 0;
        int index = -1;
        for(int i = 0; i < cntArr.length; i++) {
            if(cntArr[i] > max) {
                max = cntArr[i];
                index = i;
            }
        }

        //중복 여부 확인
        boolean isDuplicated = false;
        for(int i = 0; i < cntArr.length; i++){
            if(cntArr[i] == max && i != index) {
                isDuplicated = true;
                break;
            }
        }

        if(isDuplicated) {
            System.out.println("?");
        } else {
            //결과는 대문자로
            System.out.println((char)('A' + index));
        }
    }
}

 

7 4344 평균은 넘겠지
import java.io.*;
import java.util.StringTokenizer;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int testcase = Integer.parseInt(reader.readLine());
        StringBuilder sb = new StringBuilder();
        
        StringTokenizer st;
        for(int i = 0; i<testcase; i++){
            st = new StringTokenizer(reader.readLine());
            int N = Integer.parseInt(st.nextToken());
            Float[] score = new Float[N];
            float total = 0;
            for(int j = 0; j < N; j++){
                score[j] = Float.parseFloat(st.nextToken());
                total += score[j];
            }
            float avg = total/N;
            int count = 0;
            for(float s : score){
                if(s > avg){
                    count++;
                }
            }
            sb.append(String.format("%.3f", (float)count/N*100)).append("%\n");
        }
        reader.close();
        System.out.print(sb);
    }
}

*String.format() 사용

 

8 2941 크로아티아 알파벳
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String str = reader.readLine();
        int cnt = 0;
        while(!str.equals("")) {
            if(str.startsWith("c=") || str.startsWith("c-") || str.startsWith("d-") ||
                    str.startsWith("lj") || str.startsWith("nj") || str.startsWith("s=") || str.startsWith("z=")) {
                str = str.substring(2);
            } else if (str.startsWith("dz=")) {
                str = str.substring(3);
            } else {
                str = str.substring(1);
            }
            cnt++;
        }
        System.out.print(cnt);
    }
}

*String.startsWith() 를 사용하여 문자열 크기를 줄여가며 카운트

*한글자씩 다중 if 절로 풀어도 가능할 듯

 

9 1316 그룹 단어 체커
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        int cnt = 0;
        for(int i = 0; i < n; i++) {
            String str = reader.readLine();
            String result = "";
            boolean isGroupWord = true;
            for(int j = 0; j < str.length(); j++) {
                String c = String.valueOf(str.charAt(j));
                if(result.contains(c)){
                    if(result.indexOf(c) != result.length()-1) {
                        isGroupWord = false;
                        break;
                    }
                } else {
                    result += c;
                }
            }
            if(isGroupWord) {
                cnt++;
            }
        }
        System.out.print(cnt);
    }
}

*result: 문자의 중복이 없는 단어

*indexOf()는 문자열의 앞에서부터 일치하는 인덱스를 반환 (<-> lastIndexOf())

 

10 25206 너의 평점은
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        float scoreSum = 0;
        float creditSum = 0;

        for(int i = 0; i < 20; i++) {
            StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
            tokenizer.nextToken();
            float credit = Float.parseFloat(tokenizer.nextToken()); //과목 학점
            String grade = tokenizer.nextToken(); //등급

            switch (grade){
                case "A+":
                    scoreSum += credit * 4.5;
                    creditSum += credit;
                    break;
                case "A0":
                    scoreSum += credit * 4.0;
                    creditSum += credit;
                    break;
                case "B+":
                    scoreSum += credit * 3.5;
                    creditSum += credit;
                    break;
                case "B0":
                    scoreSum += credit * 3.0;
                    creditSum += credit;
                    break;
                case "C+":
                    scoreSum += credit * 2.5;
                    creditSum += credit;
                    break;
                case "C0":
                    scoreSum += credit * 2.0;
                    creditSum += credit;
                    break;
                case "D+":
                    scoreSum += credit * 1.5;
                    creditSum += credit;
                    break;
                case "D0":
                    scoreSum += credit * 1.0;
                    creditSum += credit;
                    break;
                case "F":
                    creditSum += credit;
                    break;
            }
        }
        System.out.println(scoreSum/creditSum);
    }
}