首页 > 解决方案 > “等待程序退出时超时”在Cs50中查找

问题描述

我还是编码新手,所以请多多包涵。我一直在试图找出我的代码出了什么问题。每当我使用 check50 -l minprog/cs50x/2020/find; 我有多个沮丧的面孔,并说它在等待程序退出时超时。谁能告诉我我做错了什么?一些微笑是向上的,大多数是向下的。我在某个地方陷入了循环吗?


#include <cs50.h>
#include <stdio.h>

#include "helpers.h"


bool search(int value, int values[], int n)
{
    // return false if n is a non-integer
    if (n < 0)
    {
        return false;
    }

    // initialize the middle index of the array
    // and set the min and max of array
       int minimum = 0, maximum = n - 1;
       int middle = (maximum - minimum) / 2 + minimum;

    // while n is a positive integer, check if the middle equals the value
    while (n > 0)
    {
        // check if the value is the same as the middle of the array
       if (value == values[middle])
       {
           return true;
       }

       // make the middle + 1 more index as the min
       // if the value is more than the middle
       else if (value > values[middle])
       {
           minimum = middle + 1;

        // make the middle - 1 less index as the max if the value is
        // less than the middle
       }
       else if (value < values[middle])
       {
           maximum = middle - 1;
       }

       // declare the new amount of numbers from the array after 'cutting'
       n = maximum - minimum + 1;
    }

    // return false if value can't be found
    return false;
}

void sort(int values[], int n)
{
    // loop over the array until i is smaller than the last index
    for (int i = 0; i < n - 1; i++)
    {
        // initialize the minimum, i starts at the beginning so it is also minimum
        int minimum = i;


        for (int j = i; j < n; j++)
        {
            // check if the minimum value is bigger than the jth value
            // if yes, set the jth int as the minimum
            if (values[minimum] > values[j])
            {
                minimum = j;
            }
        }

        // check to see if the minimum is not the same as i
        if (minimum != i)
        {
            // switch i with the lowest int
            int temporary_int = values[minimum];
            values[minimum] = values[i];
            values[i] = temporary_int;
        }
    }

    return;
}

标签: ccs50

解决方案


推荐阅读