A-C/ex7_1.c

79 lines
1.5 KiB
C
Raw Normal View History

2022-05-26 05:31:57 +00:00
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 100
void Search1(int a[], int n, int key){
int i=0;
a[n] = key;
while(a[i] != key){
i++;
}
if(i == n){
printf("\nδ<EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD>\n\n");
} else {
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> %d λ<><CEBB>\n\n", i + 1);
}
}
void Search2(int a[], int n, int key){
int low = 0, high = n-1, mid;
while(low <= high){
mid = (low + high) / 2;
if(a[mid] == key){
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> %d λ<><CEBB>\n\n", mid + 1);
return;
} else if (a[mid] > key){
high = mid - 1;
} else {
low = mid + 1;
}
}
printf("\nδ<EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD>\n\n");
}
int main(){
int n, choice, e, a[MAX];
for(;;){
printf("\t\t1.<2E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("\t\t2.<2E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1>Ķ<EFBFBD><C4B6>ֲ<EFBFBD><D6B2><EFBFBD>\n");
printf("\t\t0.<2E>˳<EFBFBD>\n");
printf("<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%d",&choice);
if(choice==0) break;
switch (choice) {
case 1:
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> %d <20><>Ԫ<EFBFBD>أ<EFBFBD>",i+1);
scanf("%d",&e);
a[i] = e;
}
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD>");
scanf("%d",&e);
Search1(a, n, e);
break;
case 2:
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> %d <20><>Ԫ<EFBFBD>أ<EFBFBD>",i+1);
scanf("%d",&e);
a[i] = e;
}
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD>");
scanf("%d",&e);
Search1(a, n, e);
break;
default:
break;
}
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>...");
getch();
system("cls");
}
printf("<EFBFBD><EFBFBD>лʹ<EFBFBD>ã<EFBFBD><EFBFBD>ټ<EFBFBD>\n");
return 0;
}