매일 Algorithm

신재원·2023년 3월 15일
0

Algorithm

목록 보기
66/243

프로그래머스 (LEVEL 1)

class Solution {
    public boolean solution(String s) {
        boolean answer = true;
        String a = s.toLowerCase();
        if (s.length() == 4 || s.length() == 6) {
            for (int i = 0; i < a.length(); i++) {
                char c = a.charAt(i);

                //  isDigit = 문자열중 숫자가 있는 경우 검증
                if (!Character.isDigit(c)) {
                    answer = false;
                    break;
                }
            }
        }
        // s의 length가 4혹은 6이 아닐경우
        else {
            answer = false;

        }
        return answer;
    }
}

프로그래머스 (LEVEL 1)

class Solution {
    public long solution(int price, int money, int count) {
        long answer = money;
        long result = 0;
        for(int i = 1; i <= count; i++){
            answer -= price * i;
        }
        if(answer > 0){
            result = 0;
        }else{
            // 부족한 금액일경우 절대값
            result = Math.abs(answer);
        }
        return result;
    }
}

프로그래머스 (LEVEL 1)

class Solution {
    public int[][] solution(int[][] arr1, int[][] arr2) {
        // arr1[0].length로 설정해주는것이 중요하다.
        int[][] answer = new int[arr1.length][arr1[0].length];
        
        for(int i = 0; i < arr1.length; i++){
            for(int j = 0 ; j < arr1[0].length; j++){
                answer[i][j] = arr1[i][j] + arr2[i][j];
            }
        }
        
        return answer;
    }
}

프로그래머스 (LEVEL 1)

import java.util.Scanner;

class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        
        for(int i = 0; i < b; i++){
            for(int j = 0; j < a; j++){
                System.out.print("*");
            }
            // 칸 띄우기
            System.out.println();
        }
    }
}

프로그래머스 (LEVEL 1)

class Solution {
    public int[] solution(int n, int m) {
        int[] answer = new int[2];
        
        answer[0] = gcd(n,m);
        answer[1] = lcm(n,m);
        return answer;
    }
    
    // 최대 공약수
    int gcd(int n, int m){
        if(m == 0) return n;
        return gcd(m, n%m);
    }
    // 최소 공배수
    int lcm (int n, int m){
        return n * m / gcd(n,m);
    }
}

프로그래머스 (LEVEL 1)

class Solution {
    public String solution(String s, int n) {
        String answer = "";

        char[] sChars = s.toCharArray();

        for(char sChar : sChars) {
            if(sChar == 32) {
                answer += " ";
            }
            else {
                // 대문자 알파벳인경우
                if(sChar <= 90) {
                    sChar += n;
                    // 대문자 Z의 범위를 벗어난 경우
                    if(sChar > 90) {
                        sChar -= 26;
                    }
                } else {
                    sChar += n;
                    // 소문자 z의 범위를 벗어난경우
                    if(sChar > 122) {
                        sChar -= 26;
                    }
                }
                answer += sChar;
            }
        }

        return answer;
    }
}

0개의 댓글