#include<stdio.h>
#define N 5
char c;
int i, n;
int flag = 1, top, bott, mid;
int loca = -1, sign = 0;
void sort(int str[])
{
while (flag)
{
printf("input number to research:\n ");
scanf("%d", &n);
top = 0;
bott = N-1;
if (n>str[0] || n<str[N - 1])
{
sign = 0;
loca = -1;
}
else
{
mid = (top + bott) / 2;
if ((n < str[mid]) || (n = str[mid]))
{
top = mid;
for (i = top; i < N; i++)
{
if (n == str[i])
{
sign = 1;
loca = i + 1;
}
}
}
else
if(n>str[mid])
{
bott = mid;
for (i = 0; i < bott; i++)
{
if (n == str[i])
{
loca = i + 1; //位置为i
sign = 1;
} //找到
}
}
}
if ((sign ==1) &&( loca != -1))
printf("\n%d local is %d\n", n, loca);
else
{
printf("NOT\n");
}
printf("是否继续寻找:(Y/N)\n");
scanf("%c", &c);
if (c == 'n' || c == 'N')
flag = 0;
}
}
int main()
{
int a[N];
int i,j,max,temp;
printf("please enter :\n");
for (i = 0; i < N; i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i < N-1 ; i++)
{
max = a[i];
for (j = i+1; j < N; j++)
{
if (max < a[j])
{
temp = max;
max = a[j];
a[j] = temp;
a[i] = max;
}
}
}
printf("this is\n");
for (i = 0; i < N; i++)
printf("%d ", a[i]);
sort(a);
return 0;
}
修改的地方都加了//
#include<stdio.h>
#define N 5
void sort(int str[])//
{//
char c;
int i, n;
int top, bott, mid;//
int loca;//
do//while (flag)
{
loca=-1;//
printf("input number to research:\n ");
scanf("%d", &n);
//top = 0;
//bott = N-1;
/*if (n>str[0] || n<str[N - 1])
{
//sign = 0;
loca = -1;
}
else*/
if (n<=str[0] || n>=str[N - 1])
{
mid =(N-1) / 2;//mid = (top + bott) / 2;
if ((n <= str[mid])) //if ((n < str[mid]) || (n = str[mid]))
{
top = mid;
for (i = top; i < N; i++)
{
if (n == str[i])
{
//sign = 1;
loca = i + 1;
break;//
}
}
}
else
if(n>str[mid])
{
bott = mid;
for (i = 0; i < bott; i++)
{
if (n == str[i])
{
loca = i + 1; //位置为i
//sign = 1;
break;//
} //找到
}
}
}
if ( loca != -1)//if ((sign ==1) &&( loca != -1))
printf("\n%d local is %d\n", n, loca);
else
{
printf("NOT\n");
}
printf("是否继续寻找:(Y/N)\n");
getchar();//
scanf("%c", &c);
//if (c == 'n' || c == 'N')
// flag = 0;
}while(!(c == 'n' || c == 'N'));//
}
int main()
{
int a[N];
int i,j,max,temp;
printf("please enter :\n");
for (i = 0; i < N; i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i < N-1 ; i++)
{
//max = a[i];
for (j = i+1; j < N; j++)
{
if (a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
//a[i] = max;
}
}
}
printf("this is\n");
for (i = 0; i < N; i++)
printf("%d ", a[i]);
printf("\n");
sort(a);
return 0;
}