728x90

import java.io.*;
import java.util.*;

public class Main {

    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); //선언

    public static void main(String[] args) throws  IOException{
        String s = bf.readLine();
        int i = Integer.parseInt(s);

        for (int j = 0; j < i; j++) {
            String s1 = bf.readLine();

            String parts[] = s1.split(" ");
            long a = Integer.parseInt(parts[0]);
            long b = Integer.parseInt(parts[1]);
            System.out.println(a*b/gcd(a,b));
        }
    }

    public static long gcd(long a, long b)
    {
        if(a%b == 0)
            return b;
        return gcd(b,a%b);
    }
}
728x90

'알고리즘' 카테고리의 다른 글

백준 알고리즘 2609  (1) 2024.03.29
백준 알고리즘 9934  (0) 2024.03.29
백준 알고리즘 2075  (0) 2024.03.29
백준 알고리즘 2800  (0) 2024.03.29
백준 알고리즘 10828  (0) 2024.03.29
728x90

 

import java.io.*;
import java.util.*;

public class Main {

    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); //선언

    public static void main(String[] args) throws IOException {
        String s= bf.readLine();
        String parts[] = s.split(" ");

        int n = Integer.parseInt(parts[0]);
        int k = Integer.parseInt(parts[1]);

        if(n==k)
        {
            System.out.println(n);
            System.out.println(n);
            return;
        }

        int big;
        int small;
        if(n>k)
        {
            big = n;
            small = k;
        }
        else {
            big = k;
            small = n;
        }

        int gcd = big*small;

        int x;
        while (small != 0)
        {
            x = big%small;
            big = small;
            small = x;
            gcd = big;
        }
        System.out.println(gcd);

        System.out.println(n*k/gcd);


    }
}
728x90

'알고리즘' 카테고리의 다른 글

백준 알고리즘 5347  (2) 2024.03.29
백준 알고리즘 9934  (0) 2024.03.29
백준 알고리즘 2075  (0) 2024.03.29
백준 알고리즘 2800  (0) 2024.03.29
백준 알고리즘 10828  (0) 2024.03.29
728x90

import java.io.*;
import java.util.*;

class pairs
{

    pairs(int i, int ii)
    {
        first = i;
        second = ii;
    }
    public int first;
    public int second;
}

public class Main {
    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); //선언

    static int arr[] = new int[10001];
    static int size;
    static ArrayList<Integer>[] tree;
    public static void main(String[] args) throws IOException {

        String n = bf.readLine();
        size  = Integer.parseInt(n);

        String n1 = bf.readLine();
        String[] parts = n1.split(" ");

        for (int j = 0; j < parts.length; j++) {
            arr[j] = Integer.parseInt(parts[j]);
        }

        tree = new ArrayList[size+1];

        for (int i = 0; i < size; i++) {
            tree[i] = new ArrayList<>();
        }

        search(0,0,parts.length-1);

        for (int i = 0; i < size; i++) {
            Iterator<Integer> iterator = tree[i].iterator();
            while (iterator.hasNext())
            {
                System.out.print(iterator.next()+" ");
            }
            System.out.println();
        }

    }


    public static void search(int level, int start, int end)
    {
        if(level == size)
            return;

        int mid = (start + end)/2;

        tree[level].add(arr[mid]);

        search(level+1, start, mid-1);
        search(level + 1, mid+1, end);
    }
}
728x90

'알고리즘' 카테고리의 다른 글

백준 알고리즘 5347  (2) 2024.03.29
백준 알고리즘 2609  (1) 2024.03.29
백준 알고리즘 2075  (0) 2024.03.29
백준 알고리즘 2800  (0) 2024.03.29
백준 알고리즘 10828  (0) 2024.03.29
728x90

import java.io.*;
import java.util.*;


public class Main {
    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); //선언

    public static void main(String[] args) throws IOException {
        String n = bf.readLine();
        int a = Integer.parseInt(n);

        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < a; i++) {
            String s = bf.readLine();
            String[] parts = s.split(" ");

            for (int j = 0; j < a; j++) {
                list.add(Integer.parseInt(parts[j]));
            }

        }

        list.sort(Integer::compareTo);

        System.out.println(list.get(list.size()-a));
    }
}
728x90

'알고리즘' 카테고리의 다른 글

백준 알고리즘 5347  (2) 2024.03.29
백준 알고리즘 2609  (1) 2024.03.29
백준 알고리즘 9934  (0) 2024.03.29
백준 알고리즘 2800  (0) 2024.03.29
백준 알고리즘 10828  (0) 2024.03.29
728x90

 

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


class pair {
    int start, end;

    public pair(int start, int end) {
        this.start = start;
        this.end = end;
    }
}

public class Main {

    static char[] input;
    static ArrayList<pair> pairs = new ArrayList<>();
    static TreeSet<String> set = new TreeSet<>();
    static boolean[] visited;
    static boolean isFirst = true;

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        Stack<Integer> stack = new Stack<>();
        input = br.readLine().toCharArray();
        for (int i = 0; i < input.length; i++) {
            char c = input[i];
            if (c == '(') {
                stack.push(i);
            } else if (c == ')') {
                pairs.add(new pair(stack.pop(), i));
            }
        }
        visited = new boolean[input.length];
        Arrays.fill(visited, true);
        dfs(0);


        for (String s : set) {
            sb.append(s).append('\n');
        }

        System.out.println(sb.toString());

        br.close();

    }

    public static void dfs(int depth)
    {
        if(depth == pairs.size())
        {
            if(isFirst)
                isFirst = false;
            else
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < input.length; i++) {
                    if(visited[i])
                        sb.append(input[i]);
                }
                set.add(sb.toString());
            }
            return;
        }

        pair pair = pairs.get(depth);

        visited[pair.start] = true;
        visited[pair.end] = true;
        dfs(depth + 1);

        visited[pair.start] = false;
        visited[pair.end] = false;
        dfs(depth + 1);
    }


}
728x90

'알고리즘' 카테고리의 다른 글

백준 알고리즘 5347  (2) 2024.03.29
백준 알고리즘 2609  (1) 2024.03.29
백준 알고리즘 9934  (0) 2024.03.29
백준 알고리즘 2075  (0) 2024.03.29
백준 알고리즘 10828  (0) 2024.03.29
728x90

import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


import java.io.IOException;
public class Main  {

    static int arr[] = new int[10001];
    static Scanner scanner = new Scanner(System.in);
    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); //선언

    public static void main(String[] args) throws IOException {

        String n = bf.readLine();
        int a = Integer.parseInt(n);

        int size = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < a; i++)
        {
            String s = bf.readLine(); //String
            // System.out.println(s);
            if(s.contains("push"))
            {
                String[] parts = s.split(" ");
                arr[size++] = Integer.parseInt(parts[1]);
            }
            if(s.equals("top"))
            {
                if(arr[0] == 0)
                    sb.append("-1\n");
                else
                    sb.append(arr[size-1] + "\n");
            }
            if(s.equals("size"))
            {
                sb.append(size+"\n");
            }
            if(s.equals("pop"))
            {
                if(arr[0] == 0)
                    sb.append("-1\n");
                else
                {
                    sb.append(arr[size-1] + "\n");
                    arr[(size--)-1] = 0;

                }
            }
            if(s.equals("empty"))
            {
                int j = arr[0] == 0 ? 1:0;
                sb.append(j + "\n");
            }
        }

        System.out.println(sb);
    }
}

 

 

728x90

'알고리즘' 카테고리의 다른 글

백준 알고리즘 5347  (2) 2024.03.29
백준 알고리즘 2609  (1) 2024.03.29
백준 알고리즘 9934  (0) 2024.03.29
백준 알고리즘 2075  (0) 2024.03.29
백준 알고리즘 2800  (0) 2024.03.29

+ Recent posts