[数据结构]栈的用法
INT 型任意十进制转化为N进制
1.数制转换-------INT 型任意十进制转化为N进制
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//数制转换,转换任意数制为十进制
int dNumericalConversion = 0;
Console.WriteLine("Input the conversion number");
if (!int.TryParse(Console.ReadLine(), out dNumericalConversion))
{
Console.WriteLine("Please input a valid number");
return;
}
Console.WriteLine("Input the conversion quotiety");
int nQuotiety = 0;
if(!int.TryParse(Console.ReadLine(),out nQuotiety))
{
if(nQuotiety <= 1) return;
Console.WriteLine("Please input a valid number");
return;
}
Stack<int> stack = new Stack<int>();
while (dNumericalConversion != 0)
{
stack.Push(dNumericalConversion % nQuotiety);
dNumericalConversion = dNumericalConversion / nQuotiety;
}
string sResult = string.Empty;
while (stack.Count > 0)
{
sResult += stack.Pop().ToString();
}
Console.WriteLine("The result is " + sResult);
2. 实现递归
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public static void Hannoi(int count, char x, char y, char z)
{
if (count == 1)
{
Console.WriteLine("Move the {2} from {0} to {1}", x,z,count);
}
else
{
Hannoi(count - 1, x, z, y);
Console.WriteLine("Move the {2} from {0} to {1}", x, z,count);
Hannoi(count - 1, y, x, z);
}
}