博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用递归的方法,判断某个字符串是否为回文
阅读量:4690 次
发布时间:2019-06-09

本文共 781 字,大约阅读时间需要 2 分钟。

回文,即一个字符串正读倒读都一样,如abcdcba

递归,就是重复使用同一种方法。

在判断字符串是否是回文的时候,如果要采用递归,首先要分析出重复做的是什么事情

这里很明显,要重复判断两端的字符是不是相等的,直到剩下最后一个或者0个字符的时候

1 #include "stdafx.h" 2 #include "stdio.h" 3 #include "string" 4 using namespace std; 5  6 int fun(char *ptr,int len) 7 { 8     if (len==1||len==0) return 1; 9     if (ptr[0]==ptr[len-1])10     {11         ptr++;12         fun(ptr,len-2);13     }14     else return 0;15 }16 17 18 19 20 int _tmain(int argc, _TCHAR* argv[])21 {22     char test[20]={
0};23 printf("please input the test string\n");24 scanf("%s",test);25 26 if (fun(test,strlen(test))) printf("yes! it is\n");27 else28 printf("no! it is not\n");29 return 0;30 }

递归的运行时间长,占用内存大,好处是代码量短

 

转载于:https://www.cnblogs.com/persistentlyworking/archive/2013/05/10/3071430.html

你可能感兴趣的文章
Python 爬虫的集中简单方式
查看>>
数据库MySQL/mariadb知识点——触发器
查看>>
Ubuntu做Tomcat服务:insserv: warning: script 'tomcat' missing LSB tags and overrides
查看>>
Binary Agents
查看>>
入门Webpack,看这篇就够了
查看>>
短信拦截马”黑色产业链与溯源取证研究
查看>>
Mac Xdebug安装时遇到了Zend Engine API 不一致的问题
查看>>
最小公倍数
查看>>
asp.net如何定时执行任务
查看>>
在github上实现页面托管预览功能
查看>>
css选择器
查看>>
prim
查看>>
给陌生人写一封信
查看>>
noip2013花匠
查看>>
[CF]Equalize Them All
查看>>
React Ant design table表单与pagination分页配置
查看>>
重大发现: windows下C++ UI库 UI神器-SOUI(转载)
查看>>
linux 压缩文件的命令总结
查看>>
linux tail 命令详解
查看>>
BZOJ-3207 花神的嘲讽计划Ⅰ
查看>>