2024-03-28 23:26:28

두 수의 최소공약수를 찾아 출력하는 문제이다. 특이한 점이라면 입력이 매우 큰 수로 주어지기 때문에, 64비트 정수를 사용해야 한다 (C#에서는 long)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
 
namespace 연습장
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
            StringBuilder sb = new StringBuilder();
 
            long[] input = Array.ConvertAll(reader.ReadLine().Split(), long.Parse);
            long A = Math.Min(input[0], input[1]);
            long B = Math.Max(input[0], input[1]);
            long temp = long.MaxValue;
 
            long a = A; long b = B;
 
            while (temp != 0)
            {
                temp = b % a;
                b = a;
                a = temp;
            }
            print.WriteLine($"{(A / b) * (B / b) * b}");
        }
 
    }
}
cs

 

유클리드 호제법을 이용해서 두 수의 최대공약수를 구한 다음 최대 공약수로 두 수를 나눈 값과 최대공약수를 곱해서 값을 출력해 줬다.