7-4
#include<stdio.h> int main(void) { int i,index,n; int a[10]; printf("Enter n:"); scanf("%d",&n); printf("Enter %d integers:",n); for(i=0;i<n;i++) scanf("%d",&a[i]); index=0; for(i=1;i<n;i++) if(a[i],a[index]) index=i; printf("min is %d sub is %d ",a[index],index); return 0; }
7-5
#include<stdio.h> int main(void) { int i,index,k,n,temp; int a[10]; printf("Enter n:"); scanf("%d",&n); printf("Enter %d intgers:",n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(k=0;k<n-1;k++){ index=k; for(i=k+1;i<n;i++) if(a[i]<a[index]) index=i; temp=a[index]; a[index]=a[k]; a[k]=temp; } printf("After sorted:"); for(i=0;i<n;i++) printf("%d",a[i]); printf(" "); return 0; }
7-6
运行结果1
结果2
#include<stdio.h> int main(void) { int a[10]={1,2,3,4,5,6,7,8,9,10}; int low,high,mid,n=10,x; printf("Enter x:"); scanf("%d",&x); low=0;high=n-1; while(low<=high){ mid=(low+high)/2; if(x==a[mid]) break; else if(x<a[mid]) high=mid-1; else low=mid+1; } if(low<=high) printf("Index is %d ",mid); else printf("Not Found "); return 0; }