【问题标题】:Why is the line 12 is printed twice?为什么第 12 行打印了两次?
【发布时间】:2015-02-25 18:00:22
【问题描述】:

给定问题

以非递归方式实现二分查找算法。 将搜索数组保留为数值数组,在声明时初始化并保持全局。 程序应该要求搜索一个值,然后告诉它找到的位置。 如果未找到该值,则程序应显示未找到。 * 此外,程序应显示为定位值而进行的比较总数(或意识到未找到该值)

我的解决方案

#include<stdio.h>
int arr[]={1,3,4,6,8,9,10,15,17,21};
int bi_search(int n)
{
    int start=0,end=9,mid=0,count=0;
    while(start<=end)
    {
        count++;
        mid=(start+end)/2;
        if(n==arr[mid])
            {
                printf("\nThe total number of comparisons done to locate the value--%d\n",count);
                return mid;
            }
        else if(n<arr[mid])
            end=mid-1;
        else
            start=mid+1;
    }
    printf("\nThe total number of comparisons done to realize that the value was not found--%d\n",count);
    return-1;
}
main()
{
    int n=0,ch=0;
    do
    {
        printf("\nEnter the value you want to search for--\n");
        scanf("%d",&n);
        if(bi_search(n)==-1)
             printf("\nSORRY :( !! \nThe value was not found.");
        else
             printf("\nHurray :) !! \nThe value you entered found at %d position", bi_search(n)+1);
        printf("\nEnter:-\n\t1 to continue.\n\t2 to terminate.\n");
        scanf("%d",&ch);
    }while(ch==1);
    printf("\nThank You\n");
    return 0;
}

当我运行此代码时,在函数 bi_search(int n) 中,只要 arr[mid] 等于 n,行 >为定位值而进行的比较总数--被打印两次。

【问题讨论】:

    标签: c arrays runtime-error binary-search


    【解决方案1】:

    您正在调用bi_search 函数两次。一次在您的 if 语句中,然后再次在 printf 语句中。您应该只调用一次,并缓存该值。

    main()
    {
        int n=0,ch=0;
        do
        {
            printf("\nEnter the value you want to search for--\n");
            scanf("%d",&n);
            if(bi_search(n)==-1) // Calling it here
                 printf("\nSORRY :( !! \nThe value was not found.");
            else
                 printf("\nHurray :) !! \nThe value you entered found at %d position", bi_search(n)+1); // And here
            printf("\nEnter:-\n\t1 to continue.\n\t2 to terminate.\n");
            scanf("%d",&ch);
        }while(ch==1);
        printf("\nThank You\n");
        return 0;
    }
    

    【讨论】:

      【解决方案2】:

      这只是因为你调用了二分搜索函数两次。 一个在 if 部分,另一个在 else if 部分。

      或者你也可以按照以下方式做同样的事情

      int temp = bi_search(n);
      if (temp == -1)
          printf("Value not found\n");
      else
          printf("Value found at position %d\n", temp);
      

      【讨论】:

        【解决方案3】:

        您的主程序可能如下所示:

        int main()
        {
           int n = 0, ch = 0;
           do
           {
              printf("\nEnter the value you want to search for--\n");
              scanf("%d", &n);
              int res = bi_search(n); // store the result
              if (res == -1) // check if not find
                 printf("\nSORRY :( !! \nThe value was not found.");
              else
                 printf("\nHurray :) !! \nThe value you entered found at %d position", res + 1); // print if find
              printf("\nEnter:-\n\t1 to continue.\n\t2 to terminate.\n");
              scanf("%d", &ch);
           } while (ch == 1);
           printf("\nThank You\n");
           return 0;
        }
        

        1) int main()(让编译器冷静)

        2) int res = bi_search(n)(避免第二次 bi_search() 调用)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-21
          • 1970-01-01
          • 2022-11-21
          • 1970-01-01
          相关资源
          最近更新 更多