C#遞歸實(shí)現(xiàn)回文判斷算法
本文實(shí)例講述了C#遞歸實(shí)現(xiàn)回文判斷算法,分享給大家供大家參考。具體實(shí)現(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#程序設(shè)計有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)Word轉(zhuǎn)換RTF的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)Word轉(zhuǎn)換RTF,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12C# 9 新特性——record的相關(guān)總結(jié)
這篇文章主要介紹了C# 9 新特性——record的相關(guān)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用c# 9的新特性,感興趣的朋友可以了解下2021-02-02