C#遞歸實現(xiàn)回文判斷算法
本文實例講述了C#遞歸實現(xiàn)回文判斷算法,分享給大家供大家參考。具體實現(xiàn)方法如下:
{
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#程序設計有所幫助。

