递归算法, 解决进制转换问题--十进制转换 N 进制, N为2, 8, 16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Digui
{
class Program
{
/// <summary>
/// 递归算法, 解决进制转换问题--十进制转换 N 进制, N为2, 8, 16
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
int i, baseNum, old;
string contin = "";
bool breakout = true;
while (breakout)
{
<pre name="code" class="csharp"><span style="white-space:pre"> </span>char[] s = new char[80];
Console.Write("Enter Base: "); baseNum = Int32.Parse(Console.ReadLine()); Console.Write("Enter oldNum: "); old = Int32.Parse(Console.ReadLine()); ; // 十进制转N进制, 2, 8, 16 Convto(s, old, baseNum, s.Length); Console.WriteLine("十进制转换 "+ baseNum + " 进制: "); for (i = 0; i <= s.Length - 1; i++) { if (s[i] != '\0') { Console.Write(" " + s[i]); } } Console.WriteLine(""); Console.WriteLine("Continue? Please Enter: yes or no "); contin = Console.ReadLine(); if (contin.ToLower() == "yes" ) { breakout = true; } else if (contin.ToLower() == "no") { breakout = false; } } Console.Write("\nStop Loop"); Console.ReadLine(); } public static void Convto(char[] s, int n, int b, int len) { char[] bit = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; //Console.Write(n + " " ); if (n == 0) { return; } Convto(s, n / b, b, len-1); s[len-2] = bit[n % b]; s[len-1] = '\0'; } }}
Result: