첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

▶ 최대 공약수 구하기 예제

using System;

GreatestCommonMeasure gcm = new GreatestCommonMeasure(49, 63);

Console.WriteLine(gcm.Value);

 

▶ 최대 공약수 구하기

using System;
using System.Collections.Generic;

/// <summary>
/// 최대 공약수
/// </summary>
public class GreatestCommonMeasure
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Field
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region Field

    /// <summary>
    /// 자연수 1
    /// </summary>
    private int naturalNumber1;

    /// <summary>
    /// 자연수 2
    /// </summary>
    private int naturalNumber2;

    /// <summary>
    /// 계산 리스트
    /// </summary>
    private List<int> calculationList;

    /// <summary>
    /// 값 (최대 공약수)
    /// </summary>
    private int value;

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 값 (최대 공약수) - Value

    /// <summary>
    /// 값 (최대 공약수)
    /// </summary>
    public int Value
    {
        get
        {
            return this.value;
        }
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - GreatestCommonMeasure(naturalNumber1, naturalNumber2)

    /// <summary>
    /// 생성자
    /// </summary>
    /// <param name="naturalNumber1">자연수 1</param>
    /// <param name="naturalNumber2">자연수 2</param>
    public GreatestCommonMeasure(int naturalNumber1, int naturalNumber2)
    {
        this.naturalNumber1 = naturalNumber1;
        this.naturalNumber2 = naturalNumber2;

        this.calculationList = new List<int>();

        Calculate();
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region 나머지 구하기 - GetRemainder(naturalNumberDividend, naturalNumberDivisor)

    /// <summary>
    /// 나머지 구하기
    /// </summary>
    /// <param name="naturalNumberDividend">자연수 피젯수</param>
    /// <param name="naturalNumberDivisor">자연수 제수</param>
    /// <returns>나머지</returns>
    private int GetRemainder(int naturalNumberDividend, int naturalNumberDivisor)
    {
        int remainder = naturalNumberDividend % naturalNumberDivisor;

        return remainder;
    }

    #endregion

    #region 계산하기 - Calculate()

    /// <summary>
    /// 계산하기
    /// </summary>
    /// <remarks>유클리드 호제법을 사용한다.</remarks>
    private void Calculate()
    {
        if(this.naturalNumber1 > this.naturalNumber2)
        {
            this.calculationList.Add(this.naturalNumber1);
            this.calculationList.Add(this.naturalNumber2);
        }
        else
        {
            this.calculationList.Add(this.naturalNumber2);
            this.calculationList.Add(this.naturalNumber1);
        }

        int i = 0;

        while(true)
        {
            int number1 = this.calculationList[i    ];
            int number2 = this.calculationList[i + 1];

            int remainder = GetRemainder(number1, number2);

            if(remainder == 0)
            {
                break;
            }

            this.calculationList.Add(remainder);

            i++;
        }

        this.value = this.calculationList[this.calculationList.Count - 1];
    }

    #endregion
}
728x90
반응형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요