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; }
#include <iostream.h>
#include <string.h>
int huiwen(char s[],int len,int i)
{
if(i>(len-1)/2)
{
if(s[i]==s[len-1-i])
return 1;
else
return 0;
}
else
{
if(s[i]==s[len-1-i])
return huiwen(s,len,i+1);//如果滿足回文條件才遞歸
else
return 0;
}
}
int main()
{
char s[20];
int len;
cin>>s;
len=strlen(s);
cout<<huiwen(s,len,0);
}
首先求出數字的長度n,
然後判斷第1位和第n位是否相等
第i位和第n+1-i位是否相等。對i循環
單獨寫一個函數求出這個數字第i位的大小
int GetI(int i,int number){
if(i==1)
return number%10;
return GetI(i-1,number/10);
}