第一个好办啊~~
用c给你做一个:
int isP(char *str)
{
char *p = str,*q = str+strlen(str)-1;
while(*p++==*q--);
if(p==q)
{
return 1;
}
else
return 0;
} 第二个是经典的算法题,代码有点长,搜索一下就可以找到,很多这个算法的资料的。
用c给你做一个:
int isP(char *str)
{
char *p = str,*q = str+strlen(str)-1;
while(*p++==*q--);
if(p==q)
{
return 1;
}
else
return 0;
} 第二个是经典的算法题,代码有点长,搜索一下就可以找到,很多这个算法的资料的。
用c给你做一个:
int isP(char *str)
{
char *p = str,*q = str+strlen(str)-1;
while(*p++==*q--);
if(p==q)
{
return 1;
}
else
return 0;
} 第二个是经典的算法题,代码有点长,搜索一下就可以找到,很多这个算法的资料的。
这只能用于奇数回文,偶数回文就不对了。if(p==q)
应该用if(p>=q)并且循环也没有退出,如果字符串是回文,你的结果将是q = str,p = str+strlen(str)-1;显然两者不等。
/*
返回1表示是回文,返回
*/
int isBackString(char *str)
{
char *head,*end;
head = str;
end = str+(strlen(str)-1);
while(*p==*q)
{
++p; --q;
}
if ((p>=q))
{
return 1;
}
else
{
return 0;
}
}
是应该用p>=q的
最不取巧的方法是,得到一个str的反向副本str1,比较str是否和str1相等,相等是回文.
using System;
using System.IO;
public class BackStr
{
public static bool IsBackStr(string str)
{
int l,m,n;
m=str.Length;
if (m==0)
{
Console.Write("你传递的字符串长度为零");
return false;
}
for(n=0,l=(m-1);n<l;n++,l--)
{
if (str[n]!=str[l])
{
return false;
}
} return true;
}
public static void Main(string[] arg)
{
int i=arg.Length;
for (int j=0;j<i ;j++ )
{
if(IsBackStr(arg[j])!=false)
Console.Write("{0} 是回文字符串!\n",arg[j]);
else
Console.Write("{0} 不是回文字符串!\n",arg[j]);
}
}};
第2个问题:可以把链表当作一个有向图,就是判断这个有向图存不存在回路。
方法:whille( true )
{
bool flag = false ; //还有边可以被去掉
for ( int iNode = 1 ; iNode <= iNodeCount ; iNode++ )
{
if ( node[iNode]只有进的边 || node[iNode]只有出的边 )
{
去掉所有与node[iNode]相连的边 ;
flag = true ;
}
}
if ( !flag )
break ;
}
if ( 所有点都是孤立点 )
没有回路 ;
else
有回路 ;