c#/백준알고리즘

[1934번 c#] 최소공배수

Heeyeon Choi 2022. 10. 3. 12:58
728x90
728x90

두 수를 곱한 값 -> 최대공약수 x 최소공배수

 

재귀를 이용하여 최대공약수와 최소공배수를 찾아준다.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project2
{
    class Class1
    {
        static void Main(string[] args)
        {
            StreamWriter writer = new StreamWriter(Console.OpenStandardOutput());
            StreamReader reader = new StreamReader(Console.OpenStandardInput());
            StringBuilder sb = new StringBuilder();

            int n = int.Parse(reader.ReadLine());
            for(int i=0; i<n; i++)
            {
                string[] input = reader.ReadLine().Split();
                int a = int.Parse(input[0]);
                int b = int.Parse(input[1]);

                writer.WriteLine((a * b) / chlth(a, b));

            }

            writer.Close();
            reader.Close();
        }

        static int chlth(int n, int m)
        {
            return (n % m == 0 ? m : chlth(m, n % m));
        }

        
    }

    
}

 

chlth 는 최대공약수를 찾아주는 함수로, 두수를 곱한 값에 최대공약수를 나눠주면 최소공배수가 된다.

728x90