매일 Algorithm

신재원·2023년 6월 21일
0

Algorithm

목록 보기
150/243

백준 12871번

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class problem482 {
    public static void main(String[] args) throws IOException {
        BufferedReader br =
                new BufferedReader(new InputStreamReader(System.in));

        String str1 = br.readLine();
        String str2 = br.readLine();

        int str1Length = str1.length();
        int str2Length = str2.length();

		
        // 문자열 누적을 위한 StringBuilder
        StringBuilder temp1 = new StringBuilder(str1);
        StringBuilder temp2 = new StringBuilder(str2);

        if(str1Length != str2Length){

			// 최대 공약수만큼 반복
            int lcm = lcm(str1Length, str2Length);

            while(temp1.length() != lcm){
                temp1.append(str1);
            }

            while(temp2.length() != lcm){
                temp2.append(str2);
            }
        }

        if(temp1.toString().equals(temp2.toString())){
            System.out.println(1);
        }else{
            System.out.println(0);
        }
    }

    private static int gcd(int a, int b) {
        if (b == 0) return a;

        return gcd(b, a % b);
    }

    private static int lcm(int a, int b) {
        return a * b / gcd(a, b);
    }
}

백준 13301번

import java.util.Scanner;

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

        int size = in.nextInt();
        if (size == 1) {
            System.out.println(4);
        } else {
            long[] arr = new long[size];

            arr[0] = 1; // 초기값 설정
            arr[1] = 1; // 초기값 설정

            for (int i = 2; i < size; i++) {
                arr[i] = arr[i - 1] + arr[i - 2]; // 점화식
            }

            long result = (2 * arr[size - 1]) + 
            		(2 * (arr[size - 1] + arr[size - 2]));
            System.out.println(result);
        }
    }
}

0개의 댓글