Day56

강태훈·3일 전

nbcamp TIL

목록 보기
56/58

코드카타 알고리즘

부족한 금액 계산하기

public class Solution {
    public long solution(int price, int money, int count) {
        long answer = 0;
        for (int i = 1; i <= count; i++) {
            answer += ((long) price * i);
        }
        if(answer <= money){
            return 0;
        }
        return answer-money;
    }
}

문자열 다루기 기본

public class Solution {
    public boolean solution(String s) {
        int length = s.length();
        if (length != 4 && length != 6){
            return false;
        }
        if (!s.matches("^[0-9]*$")){
            return false;
        }
        return true;
    }
}

행렬의 덧셈

class Solution {
    public int[][] solution(int[][] arr1, int[][] arr2) {
        int[][] answer = new int[arr1.length][arr1[0].length];
        for(int i = 0; i < arr1.length; i++){
            for(int j = 0; j < arr1[i].length; j++){
                answer[i][j] = arr1[i][j]+arr2[i][j];
            }
        }
        return answer;
    }
}

직사각형 별찍기

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 = 1; i <= b; i++) {
            for (int j = 1; j <= a; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

최대공약수와 최소공배수

class Solution {
    public int[] solution(int n, int m) {
        int[] answer = new int[2];
        int a = n;
        int b = m;
        if (n > m) {
            a = m;
            b = n;
        }

        for (int i = 1; i <= a; i++) {
            if(a%i==0&&b%i==0){
                answer[0] = i;
            }
        }

        for (int i = 1; i <= a; i++) {
            if((b*i)%a==0){
                answer[1] = b*i;
                break;
            }
        }
        return answer;
    }
}

0개의 댓글