a记录数组元素 N记录个数
#include<stdio.h>
int a[100], N;
int*find_min_element(int*begin, int*end) {
int*ret=begin, *p;
for(p=begin+1; p<end; ++p)
if(*p<*ret) ret=p;
// return the position of the min element in the range [begin,end)
return ret;
}
int*erase_element(int*pos,int*begin,int*end) {
int*p=pos;
for(;p+1<end;++p)
*p=*(p+1);
// return the position of the end of the array
return p;
}
int output(int*begin, int*end) {
int *p=begin;
for(;p<end;++p)
printf("%4d", *p);
printf("\n");
}
int main() {
int i;
// input the array
scanf("%d", &N);
for(i=0;i<N;++i) scanf("%d", a+i);
// work1
// find the position of the min element
int*p = find_min_element(a, a+N);
printf("min element = %d\n", *p);
// erase the element at position p
N=erase_element(p,a,a+N)-a;
//output the new array
printf("elment number : %d\n", N);
output(a,a+N);
// work2
p = find_min_element(a,a+N);
printf("min element = %d\n", *p);
return 0;
}
温馨提示:答案为网友推荐,仅供参考