• c语言 9-5


    1、线性查找法

    #include <stdio.h>
    
    #define FAILED -1
    
    int len(char x[])
    {
        int len = 0;
        while(x[len])
            len++;
        return len;
    }
    
    int search(char x[], char key)
    {
        int i = 0;
        while(1)
        {
            if(i == len(x))
                return FAILED;
            if(x[i] == key)
                return i;
            i++;
        }
    }
    
    int main(void)
    {
        char str[128];
        printf("str: "); scanf("%s", str);
        printf("position: %d
    ", search(str, 'c'));
        return 0;
    }

    2、线性查找法

    #include <stdio.h>
    
    #define FAILED -1
    
    int len(char x[])
    {
        int i = 0;
        while(1)
        {
            if(x[i] == '')
                return i;
            i++;
        }
    }
    
    int search(char x[], char key)
    {
        int i = 0;
        while(1)
        {
            if(i == len(x))
                return FAILED;
            if(x[i] == key)
                return i;
            i++;
        }
    }
    
    int main(void)
    {
        char str[128];
        printf("str: "); scanf("%s", str);
        printf("position: %d
    ", search(str, 'c'));
        return 0;
    }

    3、线性查找法

    #include <stdio.h>
    
    #define FAILED -1
    
    int length(char x[])
    {
        int i = 0;
        while(1)
        {
            if(x[i] == '')
                return i;
            i++;
        }
    }
    
    int search(char x[], char key)
    {
        int len, i;
        len = length(x);
        for(i = 0; i < len; i++)
        {
            if(x[i] == key)
                return i;
        }
        if(i == len)
            return FAILED;
    }
    
    int main(void)
    {
        char str[128];
        printf("str: "); scanf("%s", str);
        
        printf("position: %d
    ", search(str, 'c'));
        return 0;
    }

    4、哨兵查找法

    #include <stdio.h>
    
    #define FAILED -1
    
    int length(char x[])
    {
        int i = 0;
        while(1)
        {
            if(x[i] == '')
                return i;
            i++;
        }
    }
    
    int search(char x[], char key)
    {
        int len = length(x);
        x[len] = key;
        int i = 0;
        while(1)
        {
            if(x[i] == key)
                break;
            i++;
        }
        return i < len ? i : FAILED;
    }
    
    int main(void)
    {
        char str[128];
        printf("str: "); scanf("%s", str);
        printf("pos: %d
    ", search(str, 'c'));
        return 0;
    }

    5、简化

    #include <stdio.h>
    
    #define FAILED -1
    
    int search(char x[], char key)
    {
        int i = 0;
        while(1)
        {
            if(x[i] == key)
                return i;
            if(x[i] == '')
                return FAILED;
            i++;
        }
    }
    
    int main(void)
    {
        char str[128];
        printf("str:  "); scanf("%s", str);
        printf("pos:  %d
    ", search(str, 'c'));
        return 0;
    }

  • 相关阅读:
    三元表达式、列表推导式、生成器表达式、递归、匿名函数
    nonlocal关键字、装饰器
    函数嵌套、作用域、闭包
    实参和形参
    函数基础
    文件操作
    字符编码
    推荐一个纯JavaScript编写的图表库——Highcharts
    推荐web 前端代码的编辑分享平台——RunJS
    了解腾讯
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14811379.html
Copyright © 2020-2023  润新知