拆分数组。

编程入门 行业动态 更新时间:2024-10-06 23:25:42
本文介绍了拆分数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个数组: {100,20,-45 -345,-2 120,64,99,20,15,0,1,25} 我想把它分成两个不同的数组,这样每个数字< = 50进入左数组,每个数字50进入右数组。 我做了一些编码,但我觉得这段代码效率很低: void split_array(int * a,int size_of_array) { / * a是指向要分区的数组的指针* / int i,left_size = 0,right_size = 0; int * b,* c / *指向新数组的指针* / for(i = 0; i< size_of_array; i ++) { if(a [i]< = 50) left_size ++; if(a [i ] 50) right_size ++; } b = calloc(sizeof(* b)* left_size); c = calloc(sizeof(* c)* right_size); if(b == NULL || c == NULL) { fprintf(stderr,内存分配失败:%s%d%s",__ FILE __, __LINE __,__ func__); 退出(EXIT_FAILURE); } left_size = right_size = 0; for(i = 0; I< size_of_array; i ++) { if(a [i]< = 50) { b [left_size] = a [i]; left_size ++; } if(a [i] 50) { c [right_size] = a [i]; right_size ++; } } 退出(EXIT_SUCCESS); } 我真的不喜欢运行类似的for循环二时间。 这是不好的编程吗?

I''ve an array : {100,20, -45 -345, -2 120, 64, 99, 20, 15, 0, 1, 25} I want to split it into two different arrays such that every number <= 50 goes into left array and every number 50 goes into right array. I''ve done some coding but I feel this code is very inefficient: void split_array(int *a, int size_of_array) { /* a is the pointer to the array which is going to be partitioned */ int i, left_size =0, right_size = 0; int *b, *c /* pointers to new arrays */ for(i =0; i< size_of_array; i++) { if(a[i] <= 50) left_size++; if(a[i] 50) right_size++; } b = calloc(sizeof(*b) * left_size); c = calloc(sizeof(*c) * right_size); if( b == NULL || c == NULL) { fprintf(stderr, "memory allocation failure: %s %d %s", __FILE__, __LINE__, __func__); exit(EXIT_FAILURE); } left_size = right_size = 0; for(i =0; i< size_of_array; i++) { if(a[i] <= 50) { b[left_size] = a[i]; left_size++; } if(a[i] 50) { c[right_size] = a[i]; right_size++; } } exit(EXIT_SUCCESS); } I''m really not comfortable with running similar for loops two times. Is this bad programming ?

推荐答案

pereges写道: pereges wrote: 我有一个数组: {100,20,-45 -345,-2 120,64,99,20,15,0,1, 25} 我想把它分成两个不同的数组,每个数字< = 50进入左数组,每个数字50进入右边阵列。 I''ve an array : {100,20, -45 -345, -2 120, 64, 99, 20, 15, 0, 1, 25} I want to split it into two different arrays such that every number <= 50 goes into left array and every number 50 goes into right array.

/ * BEGIN new.c输出* / 原始数组: 100 20 -45 -345 -2 120 64 99 20 15 0 1 25 剩余阵列: -345 -45 -2 0 1 15 20 20 25 右阵: > / * BEGIN new.c * / #include< stdio.h> #include< stdlib .h> #define权利50 int compar(const void *,const void *); int main(无效) { size_t count; int array [] = {100,20,-45 ,-345,-2,120,64,99,20,15,0,1,25}; int * right = array; puts( " / * BEGIN new.c output * / \ n"); puts(" original array:"); for(count = 0; count!= sizeof array / sizeof * array; ++ count){ printf("%d",array [count]); } putchar(''\ n''); qs ort(array,sizeof array / sizeof * array,sizeof * array,compar); while(RIGHT * right && right!= array + sizeof array / sizeof * array) { ++ right; } puts(" \ nleft array:"); for(count = 0; count!= right - array + 0u; ++ count){ printf ("%d",array [count]); } puts(" \ n\\\right array:"); for(count = 0; count!= sizeof array / sizeof * array - (right - array); ++ count) { printf("%d",right [count]); } puts(" \ n\\\ / * END new.c输出* /"); 返回0; } int compar(const void * a ,const void * b) { const int * pa = a; const int * pb = b; 返回* pb * pa? -1:* pb!= * pa; } / * END new.c * / - pete

/* BEGIN new.c output */ original array: 100 20 -45 -345 -2 120 64 99 20 15 0 1 25 left array: -345 -45 -2 0 1 15 20 20 25 right array: 64 99 100 120 /* END new.c output */ /* BEGIN new.c */ #include <stdio.h> #include <stdlib.h> #define RIGHT 50 int compar(const void *, const void *); int main(void) { size_t count; int array[] = {100,20,-45,-345,-2,120,64,99,20,15,0,1,25}; int *right = array; puts("/* BEGIN new.c output */\n"); puts("original array:"); for (count = 0; count != sizeof array / sizeof *array; ++count) { printf("%d ", array[count]); } putchar(''\n''); qsort(array, sizeof array / sizeof *array, sizeof *array, compar); while (RIGHT *right && right != array + sizeof array / sizeof *array) { ++right; } puts("\nleft array:"); for (count = 0; count != right - array + 0u; ++count) { printf("%d ", array[count]); } puts("\n\nright array:"); for (count = 0; count != sizeof array / sizeof *array - (right - array); ++count) { printf("%d ", right[count]); } puts("\n\n/* END new.c output */"); return 0; } int compar (const void *a, const void *b) { const int *pa = a; const int *pb = b; return *pb *pa ? -1 : *pb != *pa; } /* END new.c */ -- pete

Keith Thompson说: Keith Thompson said: pereges< Br *** **@gmailwrites: pereges <Br*****@gmailwrites:

< snip>

<snip>

> if(a [i]< = 50) left_size ++; if(a [i] 50) right_size ++; > if(a[i] <= 50) left_size++; if(a[i] 50) right_size++;

第二次测试是不必要的。

The second test is unnecessary.

s /是不必要的/可以替换为else / < snip> - Richard Heathfield< www.cpax.uk> 电子邮件:-http:// www。 + rjh @ 谷歌用户:< www.cpax.uk/prg/writings/googly.php> Usenet是一个奇怪的放置" - dmr 1999年7月29日

s/is unnecessary/can be replaced by else/ <snip> -- Richard Heathfield <www.cpax.uk> Email: -www. +rjh@ Google users: <www.cpax.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999

Richard Heathfield< rj*@see.sig.invalidwrites: Richard Heathfield <rj*@see.sig.invalidwrites: Keith汤普森说: Keith Thompson said: > pereges< Br ***** @ gmailwrites: >pereges <Br*****@gmailwrites:

< snip>

<snip>

>> if(a [i]< = 50) left_size ++; if(a [i] 50) right_size ++; >> if(a[i] <= 50) left_size++; if(a[i] 50) right_size++;

第二次测试是不必要的。

The second test is unnecessary.

s /是不必要的/可以用else替换/ < snip>

s/is unnecessary/can be replaced by else/ <snip>

正确。 - Keith Thompson(The_Other_Keith) ks *** @ mib < www.ghoti/~kst> Nokia 我们必须做点什么。这是事情。因此,我们必须这样做。 - Antony Jay和Jonathan Lynn,是部长

Right. -- Keith Thompson (The_Other_Keith) ks***@mib <www.ghoti/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"

更多推荐

拆分数组。

本文发布于:2023-11-30 15:22:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650494.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!