目的是:要求順序輸入15個數(可有相同的數),然后輸入一個數,查找該數是否在15個數中,是則輸出其所在位置,不是則輸出無法找到。判斷完成一個數選擇是否繼續進行下一個數。
下面是我撰寫的原程式,存在兩個問題:1、無論數在與否,均無法顯示無法找到并且無法自主選擇直接進行下一個數判斷。2、一旦15個數中存在相同的數,輸入該數進行查找均顯示無法找到。
各位大佬可以幫忙分析一下,指出錯誤并提供解決辦法嗎。
#include<stdio.h>
#pragma warning(disable:4996)
#define N 15
int main()
{
int a[N], i,number,top,bott,mid,loca,sign=0,flag=1;
char c;
printf("enter %d sorted numbers:\n",N);
scanf("%d", &a[0]);
i = 1;
while(i<N)
{
scanf("%d", &a[i]);
if (a[i] < a[i - 1])
{
printf("the data is worning,please enter again:\n");
}
i++;
}
while (flag=1)
{
printf("\ninput number to look for:");
scanf("%d", &number);
top = 0; bott = N - 1;
if (number >= a[top] && number <= a[bott])
while ((top <= bott) && (sign = 0))
{
mid = (top + bott) / 2;
if (number == a[mid])
{
loca = mid + 1;
printf("has found %d,its position is %d\n", number, loca);
sign = 1;
}
else if (number < a[mid])bott = mid - 1;
else top = mid + 1;
}
else loca = -1;
if (sign == 0 || loca == -1)
printf("cannot find %d.\n", number);
printf("continue or not(Y/N)?");
scanf("%c", &c);
if (c == 'N' || c == 'n')flag = 0;
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/110401.html
標籤:疑難問題
上一篇:MATLAB2016a
下一篇:圖示的更改
