Algorithm/HackerRank

[해커랭크] Extra Long Factoriasl (C#)

Eli.P 2022. 6. 25. 22:39
728x90
반응형

https://www.hackerrank.com/challenges/extra-long-factorials/problem?isFullScreen=true 

 

Extra Long Factorials | HackerRank

Calculate a very large factorial that doesn't fit in the conventional numeric data types.

www.hackerrank.com

 

해당 문제는 BigInterger를 사용하는 문제인데 BigInterger를 구현 해서 사용하는 문제인데, 

c#에서는 BigInteger라는 정수형이 이미 존재해서 이 정수형을 사용하면 금방 구현이 가능했다.

 

https://docs.microsoft.com/ko-kr/dotnet/api/system.numerics.biginteger?view=net-6.0 

 

BigInteger 구조체 (System.Numerics)

부호 있는 임의의 큰 정수를 나타냅니다.

docs.microsoft.com

BigInteger란 부호 있는 임의의 큰 정수를 나타낸다.

 

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
using System.Numerics;

class Result
{

    /*
     * Complete the 'extraLongFactorials' function below.
     *
     * The function accepts INTEGER n as parameter.
     */

    public static void extraLongFactorials(int n)
    {
        BigInteger sum = 1;
        for(BigInteger i = n; i!=1; i--){
            sum *= i;
        }
        
        Console.WriteLine(sum);
    }

}

class Solution
{
    public static void Main(string[] args)
    {
        int n = Convert.ToInt32(Console.ReadLine().Trim());

        Result.extraLongFactorials(n);
    }
}

 

나의 풀이

1. Numerics라는 어셈플리를 참조한다.

2. BigInteger라는 정수형으로 sum이라는 초기값을 지정한다.

3. i를 n으로 지정하여 n이 1이 아닐 때 까지 감소시키며 반복한다.

4. 감소시킨 값을 sum에 곱한다. 

728x90
반응형