Java Challenge - 6

CYSSSSSSSSS·2024년 3월 18일

자바 챌린지

목록 보기
6/11

Java Challenge

심화 1

백준 25083

  • 아래 예제와 같이 새싹을 출력하시오.
package 심화1;


public class BOJ25083 {
    public static void main(String[] args) {
        System.out.println("         ,r'\"7");
        System.out.println("r`-_   ,'  ,/");
        System.out.println(" \\. \". L_r'");
        System.out.println("   `~\\/");
        System.out.println("      |");
        System.out.println("      |");
    }
}

백준 3003

  • 동혁이가 발견한 흰색 피스의 개수가 주어졌을 때, 몇 개를 더하거나 빼야 올바른 세트가 되는지 구하는 프로그램을 작성하시오.
package 심화1;

import java.util.Scanner;

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

        int [] arrs = {1,1,2,2,2,8};

        for(int i = 0; i < arrs.length; i++){
            int num = scanner.nextInt();
            System.out.print((arrs[i] - num) + " ");
        }
    }
}

백준 2444

  • 예제를 보고 규칙을 유추한 뒤에 별을 찍어 보세요.
package 심화1;

import java.util.Scanner;

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

        int n = scanner.nextInt();
        for(int i = 0; i < n; i++){
            StringBuilder sb = new StringBuilder();
            int len1 = n-i-1;
            for(int j = 0; j < len1;j++){
                sb.append(" ");
            }
            int len2 = 2 * (i+1) - 1;
            for(int k = 0; k < len2; k++){
                sb.append("*");
            }
            System.out.println(sb);
        }
        for (int i = 0; i < n-1; i++){
            StringBuilder sb2 = new StringBuilder();
            for(int j = 0; j < i+1; j++){
                sb2.append(" ");
            }
            int len3 = 2 * (n-i-1) -1;
            for(int k = 0; k < len3; k++){
                sb2.append("*");
            }
            System.out.println(sb2);
        }
    }
}

백준 10988

  • 문자열을 입력받아 팰린드롬이 맞으면 1 아니면 0을 리턴하시오
package 심화1;

import java.util.Objects;
import java.util.Scanner;

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

        String s = scanner.nextLine();
        String[]sp = s.split("");

        int len_ = (sp.length) / 2;
        boolean check = true;
        if(sp.length % 2 == 0){
            for(int i = 0; i < len_; i++){
                if(!Objects.equals(sp[i], sp[sp.length - i - 1])){
                    check = false;
                    break;
                }
            }
        }
        else{
            for(int i = 0; i <= len_; i++){
                if(!Objects.equals(sp[i] , sp[sp.length-i-1])){
                    check = false;
                    break;
                }
            }
        }

        if (!check){
            System.out.println(0);
        }else {
            System.out.println(1);
        }

    }
}

백준 1157

  • 알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
package 심화1;

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

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

        String s = scanner.nextLine();
        s = s.toUpperCase();
        int max_count = 0;
        int max = 0;
        int answer = 0;
        int []counts = new int[26];
        Arrays.fill(counts , 0);

        for(int i = 0; i < s.length(); i++){
            int idx = (int)s.charAt(i) % 65;
            counts[idx] += 1;
        }
        for(int i = 0; i < 26; i++){
            if (max < counts[i]){
                max_count = 1;
                max = counts[i];
                answer = i;
            } else if (max == counts[i]) {
                max_count++;
            }
        }

        if(max_count > 1){
            System.out.println("?");
        }else{
            System.out.println((char)(65+answer));
        }
    }
}

백준 2941

  • 단어가 주어졌을 때, 몇 개의 크로아티아 알파벳으로 이루어져 있는지 출력한다.
package 심화1;

import java.util.Scanner;

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

        String s = scanner.nextLine();

        int answer = 0;

        for(int i = 0; i < s.length(); i++){
            char word = s.charAt(i);

            if(word == 'c'){
                if(i+1 < s.length()){
                    if(s.charAt(i+1) == '='){
                        i++;
                    }else if (s.charAt(i+1) == '-'){
                        i++;
                    }
                }
            }else if(word == 'd'){
                if(i+1 < s.length()){
                    if(s.charAt(i+1) == 'z'){
                        if(i+2 < s.length()){
                            if(s.charAt(i+2) == '='){
                                i+=2;
                            }
                        }
                    }else if(s.charAt(i+1) == '-'){
                        i++;
                    }
                }
            }else if(word == 'l'){
                if(i+1 < s.length()){
                    if(s.charAt(i+1) == 'j'){
                        i++;
                    }
                }
            }else if(word == 'n'){
                if(i+1 < s.length()){
                    if(s.charAt(i+1) == 'j'){
                        i++;
                    }
                }
            }else if(word == 's'){
                if(i+1 < s.length()){
                    if(s.charAt(i+1) == '='){
                        i++;
                    }
                }
            }else if(word == 'z'){
                if(i+1 < s.length()){
                    if(s.charAt(i+1) == '='){
                        i++;
                    }
                }
            }
            answer++;
        }


        System.out.println(answer);
    }
}

백준 1316

  • 단어 N개를 입력으로 받아 그룹 단어의 개수를 출력하는 프로그램을 작성하시오.
package 심화1;

import java.util.Scanner;

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

        int count = scanner.nextInt();
        int answer = 0;
        for(int i = 0; i <count; i++){
            String s = scanner.next();
            boolean check = true;
            for(int j = 0; j<s.length();j++){
                char w = s.charAt(j);
                for(int k = 0; k<j; k++){
                    if (w !=s.charAt(j-1) && s.charAt(k) == w){
                        check = false;
                        break;
                    }
                }
            }
            if (check){
                answer++;
            }
        }

        System.out.println(answer);
    }
}

백준 25206

  • 치훈이의 전공평점을 계산해주는 프로그램을 작성해보자.
package 심화1;

import java.util.Scanner;

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

        double credit_sum = 0;
        double score_sum = 0;
        for(int i = 0; i < 20; i++){
            String info = scanner.nextLine();
            String[]sp = info.split(" ");

            double score;
            double credit = Double.parseDouble(sp[1]);
            if (sp[2].equals("A+")){

                score = 4.5;
            } else if (sp[2].equals("A0")) {

                score = 4.0;
            } else if (sp[2].equals("B+")) {

                score = 3.5;
            } else if (sp[2].equals("B0")) {

                score = 3.0;
            } else if(sp[2].equals("C+")){

                score = 2.5;
            } else if(sp[2].equals("C0")){

                score = 2.0;
            } else if(sp[2].equals("D+")){

                score = 1.5;
            } else if(sp[2].equals("D0")){

                score = 1.0;
            } else if (sp[2].equals("P")) {
                continue;
            }else{

                score = 0.0;
            }
            credit_sum += credit;
            score_sum += (score * credit);
        }
        System.out.println(score_sum / credit_sum);
    }
}
profile
개발자 되고 싶어요

0개의 댓글