📌Back-End/프로그래머스

Java 프로그래머스 코딩테스트 입문 Day 7 문자열, 조건문, 수학, 반복문

구 일 2024. 6. 28. 19:10
728x90
반응형

 

특정 문자 제거하기

프로그래머스 특정 문자 제거하기 자바

 

 

/**
 * 코딩테스트 입문 Day 7 문자열, 조건문, 수학, 반복문
 * 특정 문자 제거하기
 */
public class Main1 {
    public static void main(String[] args) {
        System.out.println(solution("abcdef", "f"));
        System.out.println(solution("BCBdbe", "B"));
    }

    public static String solution(String my_string, String letter) {
        return my_string.replace(letter, "");
    }
}

 

 

각도기

프로그래머스 각도기 자바

 

 

/**
 * 코딩테스트 입문 Day 7 문자열, 조건문, 수학, 반복문
 * 각도기
 */
public class Main2 {
    public static void main(String[] args) {
        System.out.println(solution(70));
        System.out.println(solution(91));
        System.out.println(solution(180));
    }

    public static int solution(int angle) {
        int answer = 0;

        if (angle == 180) {
            answer = 4;
        } else if (angle < 180 && angle > 90) {
            answer = 3;
        } else if (angle == 90) {
            answer = 2;
        } else {
            answer = 1;
        }

        return answer;
    }
}

 

 

양꼬치

프로그래머스 양꼬치 자바

 

 

/**
 * 코딩테스트 입문 Day 7 문자열, 조건문, 수학, 반복문
 * 양꼬치
 */
public class Main3 {
    public static void main(String[] args) {
        System.out.println(solution(10, 3));
        System.out.println(solution(64, 6));
    }

    public static int solution(int n, int k) {
        int food = 12000;
        int drink = 2000;
        int service = n / 10;
        k -= service;

        return (food * n) + (drink * k);
    }
}

 

 

짝수의 합

프로그래머스 짝수의 합 자바

 

 

import java.util.stream.IntStream;

/**
 * 코딩테스트 입문 Day 7 문자열, 조건문, 수학, 반복문
 * 짝수의 합
 */
public class Main4 {
    public static void main(String[] args) {
        System.out.println(solution(10));
        System.out.println(solution(4));
    }

    public static int solution(int n) {
        return IntStream.rangeClosed(1, n).filter(i -> i % 2 == 0).sum();
    }
}

 

 

728x90
반응형