본문 바로가기
알고리즘/백준 BOJ

[ BOJ/C# ] 2609 최대공약수와 최소공배수

by 왹져박사 2023. 10. 16.
728x90

https://www.acmicpc.net/problem/2609

 

2609번: 최대공약수와 최소공배수

첫째 줄에는 입력으로 주어진 두 수의 최대공약수를, 둘째 줄에는 입력으로 주어진 두 수의 최소 공배수를 출력한다.

www.acmicpc.net

오랜만에, 간단한 문제지만 꼭 해봐야 할 문제로 생각되는 최대공약수와 최소공배수 문제를 풀었다. 

유클리드 호제법만 안다면 간단하게 풀 수 있다. 

using System;
using System.IO;
using System.Text;

namespace B2609
{
    class Program
    {
        static int Cal(int a, int b)
        {
            int c;
            while (b != 0)
            {
                c = a % b;
                a = b;
                b = c;
            }
            return a;
        }
        static void Main()
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            int[] ints = Array.ConvertAll(sr.ReadLine().Split(' '), int.Parse);
            int max = Cal(ints[0], ints[1]);
            int min = (ints[0] * ints[1]) / Cal(ints[0], ints[1]);
            sw.Write(max + "\n" + min);

            sr.Close();
            sw.Flush();
            sw.Close();
        }
    }
}

728x90

'알고리즘 > 백준 BOJ' 카테고리의 다른 글

[ BOJ/C# ] 2231 분해합  (0) 2023.10.18
[ BOJ/C# ] 2292 벌집  (0) 2023.10.17
[ BOJ/C# ] 10026 적록색약  (0) 2023.10.14
[ BOJ/C# ] 2667 단지번호붙이기  (0) 2023.10.13
[ BOJ/C# ] 2178 미로 탐색  (0) 2023.10.12