我如何显示所有 3 个值,如一个低索引、一个高索引和最后一个是最高的,但找不到?
How can i show all the 3 values like one low index, one high and last one is the highest which wont be found?
在迭代过程中实现二分查找。一个一个地搜索 3 个值,第一个将放置在低索引和中索引中,第二个将放置在中索引和高索引中,最后一个值将是数组的最大值并且不会找到了。
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter first value to find\n");
scanf("%d", &search);
printf("Enter second value to find\n");
scanf("%d", &search);
printf("Enter third value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
正确的方法是将应针对不同值重复的代码放在循环或函数中。在这里你可以很容易地使用一个循环:
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
const char* label[] = { "first", "second", "third" };
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
// idiomatic way for an array length
unsigned repeat = sizeof(label) / sizeof(*label);
for (unsigned count = 0; count < repeat; count++) {
printf("Enter %s value to find\n", label[count]);
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first + last) / 2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle + 1);
break;
}
else
last = middle - 1;
middle = (first + last) / 2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
}
return 0;
}
但是您应该始终控制输入函数 (scanf
) 的 return 值:如果您键入非数字字符(例如 a
) 以下所有 scanf
将 return 0
而不是 1 并保持变量不变。然后,您的代码将使用未初始化的值来调用未定义的行为(C 程序员的地狱...)。
你所有的 scanf
调用应该如下所示:
if (1 != scanf("%d", &n)) {
// process error
fprintf(stderr, "Incorrect input\n");
exit(1);
}
您将以有序的方式停止程序并警告用户原因,而不是冒着崩溃或意外结果而没有错误消息的风险。
这里您实际上是在搜索最后一个值。查看代码的以下部分:
printf("Enter first value to find\n");
scanf("%d", &search);
printf("Enter second value to find\n");
scanf("%d", &search);
printf("Enter third value to find\n");
scanf("%d", &search);
您使用同一个变量来存储所有三个整数。因此,在您每次输入之后,搜索的值都会被当前输入所取代。如果您想对所有搜索输入使用单个变量。然后你可以在循环中调用它。请参阅下面的示例以更好地理解:
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for(i = 0; i < 3; i++){
printf("Enter first value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
}
return 0;
}
在迭代过程中实现二分查找。一个一个地搜索 3 个值,第一个将放置在低索引和中索引中,第二个将放置在中索引和高索引中,最后一个值将是数组的最大值并且不会找到了。
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter first value to find\n");
scanf("%d", &search);
printf("Enter second value to find\n");
scanf("%d", &search);
printf("Enter third value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
正确的方法是将应针对不同值重复的代码放在循环或函数中。在这里你可以很容易地使用一个循环:
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
const char* label[] = { "first", "second", "third" };
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
// idiomatic way for an array length
unsigned repeat = sizeof(label) / sizeof(*label);
for (unsigned count = 0; count < repeat; count++) {
printf("Enter %s value to find\n", label[count]);
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first + last) / 2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle + 1);
break;
}
else
last = middle - 1;
middle = (first + last) / 2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
}
return 0;
}
但是您应该始终控制输入函数 (scanf
) 的 return 值:如果您键入非数字字符(例如 a
) 以下所有 scanf
将 return 0
而不是 1 并保持变量不变。然后,您的代码将使用未初始化的值来调用未定义的行为(C 程序员的地狱...)。
你所有的 scanf
调用应该如下所示:
if (1 != scanf("%d", &n)) {
// process error
fprintf(stderr, "Incorrect input\n");
exit(1);
}
您将以有序的方式停止程序并警告用户原因,而不是冒着崩溃或意外结果而没有错误消息的风险。
这里您实际上是在搜索最后一个值。查看代码的以下部分:
printf("Enter first value to find\n");
scanf("%d", &search);
printf("Enter second value to find\n");
scanf("%d", &search);
printf("Enter third value to find\n");
scanf("%d", &search);
您使用同一个变量来存储所有三个整数。因此,在您每次输入之后,搜索的值都会被当前输入所取代。如果您想对所有搜索输入使用单个变量。然后你可以在循环中调用它。请参阅下面的示例以更好地理解:
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for(i = 0; i < 3; i++){
printf("Enter first value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
}
return 0;
}