软件编程
位置:首页>> 软件编程>> C#编程>> C#递归实现回文判断算法

C#递归实现回文判断算法

作者:shichen2014  发布时间:2022-06-14 13:45:16 

标签:C#,递归,算法

本文实例讲述了C#递归实现回文判断算法,分享给大家供大家参考。具体实现方法如下:

static void Main(string[] args)
{
    DateTime dt1 = DateTime.Now;

    string text = "abcdedcba";
    bool bYes = Recv(text);
    Console.Write("{0}:{1}回文!", text, bYes ? "是" : "不是");

    DateTime dt2 = DateTime.Now;
    Console.Write("耗时:{0}毫秒", (dt2 - dt1).TotalMilliseconds.ToString());
    Console.ReadLine();
}

private static bool Recv(string text)
{
    string head = text.Substring(0, 1);
    string end = text.Substring(text.Length - 1, 1);
    if (head == end)
    {
 if (text.Length == 1)
     return true;
 string t = text.Substring(1, text.Length - 2);
 return Recv(t);
    }
    return false;
}

希望本文所述对大家的C#程序设计有所帮助。

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com