zoukankan      html  css  js  c++  java
  • 简单递归____判断一个字符串是否为回文

    1.递归的作用在于把问题的规模不断缩少,直到问题缩少到能简单地解决

    2.新问题与原问题有着相同的形式

    3.递归的结束需要简单情景

     

    解法:我们只需要以去掉两端的字符的形式一层层检查,每一次的检查都去掉了两个字符,这样就达到了缩少问题规模的目的。

     

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    void fun(char *array, int arraySize, int index)
    {
        //printf("index:%d\n",index);
        //printf("size:%d\n",arraySize);
        //printf("%c\n",*(array + index));
        //printf("%c\n",*(array + arraySize - index - 1));
        if( (index * 2) >= arraySize)
        {
            printf("是回文序列!\n");
            return;
        }
        else if ( *(array + index) != *(array + arraySize - 1 - index) )
        {
            printf("不是回文序列!\n");
            return;
        }
        fun(array, arraySize, ++index);
    }
    
    int main()
    {
        //例如”heooeh”是回文,而”hello”则不是回文。用一递归函数实现,并在main中验证。
        char a1[] = "heooeh";
        char a2[] = "hello";
    
        fun(a1, strlen(a1), 0);
        fun(a2, strlen(a2), 0);
        return 0;
    }


     

  • 相关阅读:
    Python爬虫-05:Ajax加载的动态页面内容
    Python爬虫-04:贴吧爬虫以及GET和POST的区别
    Python-爬虫03:urllib.request模块的使用
    Python Numpy-基础教程
    8皇后算法
    迷宫算法
    归并排序
    查找算法
    排序算法
    设计模式
  • 原文地址:https://www.cnblogs.com/wwjyt/p/3153132.html
Copyright © 2011-2022 走看看