.NET 으로 구현
1. while문 으로 처리
public partial class Default : System.Web.UI.Page
{
protected int targetN = 0;
protected int intN = 0;
protected string strRtn = "";
protected void Page_Load(object sender, EventArgs e)
{
targetN = 233;
intN = 16;
strRtn = mf_ConvertN(targetN,intN);
}
protected string mf_ConvertN(int _targetN, int _intN)
{
bool End = true;
string rtn ="";
int a=0; // 몫
int b=0; // 나머지
List <int> ConvertRtn = new List<int>();
while (End) {
int starget = ConvertRtn.Count > 0 ? a : _targetN ;
a = starget / _intN;
b = starget % _intN;
ConvertRtn.Add(b);
if (a < _intN)
{
End = false; // N진수 보다 작아질 경우 빠져나온다
}
}
rtn = a.ToStringExt("");
for(int i = (ConvertRtn.Count-1); i>=0; i--){
rtn += ConvertRtn.ElementAt(i).ToStringExt("");
}
return rtn;
}
}
2. 재귀 호출
#region N진수 변환 작업
public class Algorism_Dt
{
List<int> ConvertRtn = new List<int>(); // 나머지 저장 list
string strRtnVal = ""; // 결과 값
#region N진수 변환 함수
public string mf_ConvertN(int _targetN, int _intN, string _strRtnVal)
{
if (_strRtnVal != "") // 결과 값이 나왔을때
{
strRtnVal = _strRtnVal;
return strRtnVal;
}
else {
int intQt = 0; // 몫
int intRm = 0; // 나머지
int intTargetValue = _targetN; // 변환 값
intQt = intTargetValue / _intN;
intRm = intTargetValue % _intN;
ConvertRtn.Add(intRm);
if (intQt <= _intN) // 빠져 나옴
{
strRtnVal = intQt.ToStringExt("");
for (int i = (ConvertRtn.Count - 1); i >= 0; i--)
{
strRtnVal += ConvertRtn.ElementAt(i).ToStringExt("");
}
mf_ConvertN(intQt, _intN, strRtnVal); // 결과 값 리턴 재귀 호출
}
else
{
mf_ConvertN(intQt, _intN, strRtnVal); // 재귀 호출
}
return strRtnVal;
}
}
#endregion
}
#endregion
재귀 함수 장/단점
장 1. 소스코드 길이가 짧아진다.
2. 프로그램의 개념 잡기용으로 많이 나옵니다.
단 1.소스를 이해하는데 시간이 걸린다.
2. 느리다.
문제 출처 : http://codingdojang.com/scode/458#answer-filter-area