【C学习笔记】day5-2 写代码可以在整型有序数组中查找想要的数字, 找到了返回下标,找不到返回-1.(折半查找)

#include <stdio.h>
#include <stdlib.h>
int find(int s)
{
    int n = 0;
    scanf_s("%d",&s);
    int a[100] = { 213,432,5436,765324,243,654,12387,87626,645,625,5234,246 };
    for (int i = 0; i < 100; i++)
    {
        if (s == a[i])
        {
            return i;
            break;
        }
        else
            n++;
    }
    if (n == 100)
        return -1;
}

int main()
{
    while (1)
    {
        int m = find(0);
        printf_s("%d\n", m);
    }
    return 0;
}

 

 

posted @ 2021-12-16 13:45  2021yer  阅读(29)  评论(0编辑  收藏  举报