拆分数组。 [英] splitting an array.

查看:58
本文介绍了拆分数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:


{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 *** **@gmail.comwrites:
pereges <Br*****@gmail.comwrites:



< 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< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

s/is unnecessary/can be replaced by else/

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.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 ***** @ gmail.comwrites:
>pereges <Br*****@gmail.comwrites:



< 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.org < http://www.ghoti.net/~kst>

Nokia

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

Right.

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


这篇关于拆分数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆