1、基础算法题

1.递归反向输出字符串

#include<iostream>
using namespace std;

void inverse(char*p){  //*p = 'http' ; 
    
    /*
    算法原理:
    将‘http’拆分成‘h’‘t’‘t’‘p’判断是否为空字符,不为空则继续递归进行比较,
    比较完之后在通过
    inverse(p + 1);
    printf("%c", *p);
    逐个进行打印
    
    */
    if (*p == '\0')  //表示字符型指针p所指向的变量值不为空字符
    { 
        return;
    }
    inverse(p + 1);
    printf("%c", *p);
}

int main(){

    inverse("http");
    system("pause");
    return 0;
}

2、用递归算法判断数组a[n]是否为一个递增数组

方法:记录当前最大的,并且判断当前的是否比这个还大,大则继续,否则返回false结束

#include<iostream>
using namespace std;

/*
    思路:
    a为数组,n为数组中数字的个数
    当数组的个数大于3个时
    首先执行
    return fun(a, n - 1) && a[n - 1] > a[n - 2];
    判断数组是否递增
    若递增则继续进行递归,直到n=2和n=1时开始执行上面的两个if语句
*/
bool fun(int* a, int n)
{
    if (n == 1){
        return true;
    }
    if (n == 2){
        return a[n - 1] >= a[n - 2];
    }
    return fun(a, n - 1) && a[n - 1] > a[n - 2];
}


int main()
{
    int a[] = { 1, 2, 3, 8, 5 };
    int n = 5;
    if (fun(a, 5) == true)
    {
        printf("True");
    }
    else{
        printf("False");
    }
    
    system("pause");
    return 0;

}

3、实现strcmp

什么是Strcmp?

设这两个字符串为str1,str2,
若str1=str2,则返回零;
若str1<str2,则返回负数;
若str1>str2,则返回正数。
#include<iostream>
using namespace std;
/*
    思路:
    整个函数的原型中得益于“(*str1!='\0')&&(*str1==*str2)”这一句的代码,
    因为这样当字符指针指向空,意味着字符串已经移动到最后了,比较结束,
    此时可以退出循环。而如果两个字符串不相等时,则此时函数也可以退出了。
*/
void strmp(const char* str1, const char* str2)
{
    while ((*str1 != '\0')&&(*str1 == *str2)) 
    {
        str1++;    /*字符右移*/
        str2++;
    }
    if ((*str1 - *str2) > 0){
        printf("str1>str2");
    }
    if ((*str1 - *str2) == 0){
        printf("str1=str2");
    }
    if ((*str1 - *str2) < 0){
        printf("str1<str2");
    }        
}
int main()
{
    strmp("https","htts");
    system("pause");
    return 0;

}

4、实现子串定位

方法一:

/*
    思路:
        1、判断字符串是否为空
        2、然后将T和S的字符逐渐进行比较
        3、不匹配时,执行
        i = i - j + 1;
        j = 0;
        4、最终通过return j == Slength ? i - Slength : -1;得到结果
*/
int findstr(char T[],char S[],int Tlength,int Slength)
{
    if (T == NULL&&S == NULL)
    {
        return -1;
    }
    int j = 0;
    int i = 0;
    while (i < Tlength&&j < Slength)
    {
        if (T[i] == S[j])
        {
            i++;
            j++;
        }
        else{
            i = i - j + 1;
            j = 0;
        }
    }
    return j == Slength ? i - Slength : -1;
}

方法二:

int findSubStr(const char* MainStr,const char* SubStr)
{
    const char* p;
    const char* q;
    const char* u = MainStr;
    if(MainStr == NULL || SubStr == NULL)
    {
        return -1;
    }
    while(*MainStr)
    {
        p = MainStr;
        q = SubStr;
        //找到子串的位置,当*p!=*q时循环跳出
        while(*p && *q && *p++ == *q++)
        if(!*q)
        {
            return MainStr-u+1;
        }
        MainStr++;
    }
    return -1;
}

复杂度最低。运算最快的方法:

KMP算法,该算法会在本栏里讲解

 

转载于:https://www.cnblogs.com/zhuifeng-mayi/p/10639127.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值