728x90
728x170
▶ 최대 공약수 구하기 예제
using System;
GreatestCommonMeasure gcm = new GreatestCommonMeasure(49, 63);
Console.WriteLine(gcm.Value);
728x90
▶ 최대 공약수 구하기
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
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON] 논리적 볼륨 시리얼 번호 구하기 (0) | 2014.11.30 |
---|---|
[C#/COMMON] 물리적 볼륨 시리얼 번호 구하기 (0) | 2014.11.30 |
[C#/COMMON] Ping 클래스 사용하기 (0) | 2014.11.30 |
[C#/COMMON] 최소 공배수 구하기 (0) | 2014.11.30 |
[C#/COMMON] 최대 공약수 구하기 (0) | 2014.11.30 |
[C#/COMMON] 팩토리얼 구하기 (0) | 2014.11.30 |
[C#/COMMON] 네트워크 드라이브 연결 끊기 (0) | 2014.11.30 |
[C#/COMMON] 네트워크 드라이브 연결하기 (0) | 2014.11.30 |
[C#/COMMON] MethodBase 클래스 : GetCurrentMethod 정적 메소드를 사용해 현재 실행 중인 메소드명 구하기 (0) | 2014.11.30 |
[C#/COMMON] 최소 제곱법을 사용해 회귀분석 수식 구하기 (0) | 2014.11.30 |