首页后端开发其他后端知识c语言自定义函数查找数组 c语言在数组中查找指定元素

c语言自定义函数查找数组 c语言在数组中查找指定元素

时间2023-07-08 23:53:01发布访客分类其他后端知识浏览564
导读:C语言查找数组中的数据 #define IntSize sizeof(int #define StructSize sizeof(struct tagresult #includestdio.h#includestdlib.h#includ...

C语言查找数组中的数据

#define IntSize sizeof(int)

#define StructSize sizeof(struct tagresult)

#includestdio.h

#includestdlib.h

#includestring.h

typedef int *ptint;

typedef struct tagresult

{

int v;

char bl;

} *ptresult;

int lessthan(const void *v1,const void *v2)

{

int i1=*((ptint)v1),i2=*((ptint)v2);

if(i1==i2)

return 0;

else if(i1衡简i2)

return 1;

else

return -1;

}

int main()

{

int c,n,capacity=128,rlen=0,dlen;

ptint data=(ptint)calloc(capacity,IntSize);

ptresult result;

scanf("%d",n);

result=(ptresult)calloc(n,StructSize);

memset(result,0,n*StructSize);

while(n--0)

{

scanf("%d",c);

dlen=0;

for(; c0; c--)

{

if(dlen+1=capacity)

{

capacity*=2;

data=(ptint)realloc(data,capacity);

}

scanf("%d",data+dlen++);

}

scanf("%d",c);

//直接调用库函数qsort进行弯拦晌快速排序,就不自己写快速排埋锋序算法函数了

qsort(data,dlen,IntSize,lessthan);

if(c=dlen)

{

(*(result+rlen)).v=*(data+c-1);

(*(result+rlen)).bl=1;

}

rlen++;

}

for(n=0; nrlen; n++)

{

if(1==(*(result+n)).bl)

printf("Case #%d:%d\n",n+1,(*(result+n)).v);

else

printf("Case #%d:-1\n",n+1);

}

free(data);

free(result);

return 0;

}

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: c语言自定义函数查找数组 c语言在数组中查找指定元素
本文地址: https://pptw.com/jishu/296959.html
c语言用函数给结构体赋值 c语言如何给结构体赋值 log函数在C语言 log函数c语言公式

游客 回复需填写必要信息