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;
    }


     

  • 相关阅读:
    sublime text 前端插件安装
    echarts常用的配置项
    2018年okr
    charlse配置
    运维笔记
    移动端开发兼容问题全记录
    centos6下python开发环境搭建
    centos安装python2.7
    centos6安装MariaDB
    pzea上centos6安装mysql57
  • 原文地址:https://www.cnblogs.com/wwjyt/p/3153132.html
Copyright © 2011-2022 走看看