求出指定数的大于它的最小数或者小于它的最大数
求出指定数的大于它的最小数或者小于它的最大数
#include <stdio.h> int main() { int arr[8]={21,24,7,9,40,25,6,90}; int i,j,tmp; for(i=0;i<8;i++) { for(j=i+1;j<8;j++) { if(arr[i]<arr[j]) { tmp=arr[i]; arr[i]=arr[j]; arr[j]=tmp; } } } int b=23; for(i=0;i<8;i++) { if(arr[i]<=b) break; } printf("小于本身最大值是%4d 大于本身最小值是%4d\n",arr[i],arr[i-1]); return 0; }
#include <stdio.h> void foo( int arr[], size_t n, int value, int** restrict pless, int** restrict pgreater ) { *pless=NULL, *pgreater=NULL; for( size_t i=0; i!=n; ++i ) { if( arr[i] < value ) { if( !*pless || arr[i]>**pless ) *pless = &arr[i]; } else if( arr[i] > value ) { if( !*pgreater || arr[i]<**pgreater ) *pgreater = &arr[i]; } } } int main( void ) { int Array[8] = { 1,4,7,9,40,23,6,90 }; int *pless, *pgreater; foo( Array, sizeof(Array)/sizeof(Array[0]), 11, &pless, &pgreater ); if( pless ) printf( "%d\n", *pless ); if( pgreater ) printf( "%d\n", *pgreater ); return 0; }
#include <stdio.h> void main() { int i,c=11,limax,lamin,a[]={1,4,7,9,40,23,6,90}; for(i=0,limax=lamin=a[0];i<8;i++) { if((a[i]>c&&a[i]<lamin)||lamin<c)lamin=a[i]; if((a[i]<c&&a[i]>limax)||limax>c)limax=a[i]; } printf("紧挨着%d的两个数是%d,%d\n",c,limax,lamin); }