一、N-S流程图;
二、运行结果;
三、源代码;
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
//定义数据;
#define N 15int main()
{//初始化变量值;int a[N], i, top, bott, loca, flag = 1, sign, number, mid;char c;//填充数据;a[0] = 1;i = 1;//循环填充数;while (i < N){//填充;printf("a[%d]=", i);scanf("%d", &a[i]);//判断;if (a[i] >= a[i - 1]){//改变变量值;i++;}else{//提示用户;printf("您输入的数据不符合要求!!!\n");printf("请重新输入:\n");}}//换行;printf("\n");//循环打印数组;for (i = 0; i < N; i++){//打印;printf("%5d", a[i]);}//换行;printf("\n");//判断数据是否在数组中;while (flag){//获取数据;printf("请输入您要查找的数据:");scanf("%d", &number);//赋值;sign = 0;top = 0;bott = N - 1;//判断数据是否在数组内;if ((number < a[0]) || (number > a[N - 1])){//改变变量值;loca = -1;}else{//折半查找;while ((sign == 0) && (top <= bott)){//求出中间下标值;mid = (top + bott) / 2;//比较;if (number == a[mid]){//改变变量值;loca = mid;sign = 1;//输出值;printf("您输入的数据%d在数组中是第%d个元素值\n", number, loca + 1);}else if (number < a[mid]){//改变变量值;bott = mid - 1;}else{//改变变量值;top = mid + 1;}}}//判断;if ((sign == 0) || (loca == -1)){//输出结果;printf("您输入的数值%d在数组中无结果!!!\n", number);}//是否结束程序;printf("是否要结束查找?(Y/N)\n");scanf(" %c", &c);//判断;if (c == 'N'){//改变变量值;flag = 0;}}return 0;
}