c - “等待程序退出时超时”在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;
}
解决方案
推荐阅读
- c++ - 在自己的类中访问 toString() 非成员函数
- javascript - 简单模态不会在点击时显示
- sql-server - 用枚举数据初始化表
- html - 无法访问自定义 Front Matter 变量
- c++ - 将字符从一个字符串简单分配到另一个字符串不起作用
- javascript - 从状态中删除特定项目
- c - 转换为兼容返回类型的联合会满足函数指针的兼容性标准吗?
- mysql - 按列绑定两个 SQL 查询的结果
- c++ - QThread::create 在 UI 线程上运行
- c# - 用于转换数值的表达式:System.InvalidCastException:> 无法将“System.Int32”类型的对象转换为“System.Int64”类型