#include<stdio.h> int main() { int a[12]={3,5,8,9,14,17,19,21,23,26,28,32}; int x,i,top,bot,mid,find; scanf("%d",&x); top=0; bot=11; find=0; do { mid=(top+bot)/2; if(x==a[mid]) { printf("found: %d,it is a[%d].\n",x,mid); find=1; break; } else if(x<a[mid]) bot=mid-1; else top=mid+1; } while(top<=bot); if(!find) printf("%d not found.\n",x); return 0;
时间: 2024-04-04 13:29:36 浏览: 23
这是一个简单的 C 语言程序,它使用二分查找算法在数组 a 中查找输入的数 x。程序中定义了一个包含 12 个元素的整数数组 a,并且初始化了数组中的元素。接着程序使用 scanf 函数从控制台读取一个整数 x,作为要查找的数。程序接着定义了变量 top、bot、mid 和 find,它们分别表示数组的上界、下界、中间位置和是否找到输入的数。程序通过一个 do-while 循环不断缩小查找范围,直到找到输入的数或者查找范围为空。如果找到了输入的数,程序输出提示信息,否则输出未找到的信息。最后程序返回 0,表示程序运行成功结束。
相关问题
#include+<stdio.h> #include+<stdlib.h> #include<string.h> #define+MAX_NAME+5 #define+MAX_VERTEX_NUM
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME 5
#define MAX_VERTEX_NUM 40
int main() {
// Your code here
return 0;
}
```
这是一个简单的C程序的框架,包含了头文件`stdio.h`、`stdlib.h`和`string.h`,以及定义了常量`MAX_NAME`和`MAX_VERTEX_NUM`。你可以在`// Your code here`的位置编写你的具体代码逻辑。这个程序的作用是创建一个空的C程序框架,你可以在其中添加你需要的功能。
#include <stdio.h>int main() { int a=1; a++; printf("%d",a); printf("%d",a++);
这段代码还是存在错误,仍然缺少了代码的结尾大括号 '}'。如果将代码修改为:
```c
#include <stdio.h>
int main() {
int a=1;
a++;
printf("%d",a);
printf("%d",a++);
return 0;
}
```
那么输出结果应该是:23。因为 `a++` 的值是先返回再自增,所以第二个 printf 输出的是 2,而 a 的值已经自增为 3。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)