两个较短阵列中的较长阵列 [英] Longer array out of two shorter arrays

查看:69
本文介绍了两个较短阵列中的较长阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的问题是:有两个单维数组,更长[M]和

更短[N],每个数组都以升序排列订购。我们需要

从两个中构建一个新数组。这就是我写的:

#include< stdio.h>

#define M 8

#define N 5

main()

{

int big [M] = {1,2,5,8,10,23,45,56};

int small [N] = {3,7,11,12,100};

int new [M + N];

int i = 0, k = 0,l = 0;


while(l<(N + M))

{

if(small [k]< big [i])

{

new [l] = small [k];

k ++;

l ++;

}

else

{

new [l] = big [i ];

i ++;

l ++;

}

}


for(l = 0; l<(M + N); l ++)

printf("%d",new [l]);

printf (" \ n");

}


输出结果为:


[Mike @ localhost演习] $ ./153

1 2 3 5 7 8 10 11 12 23 45 56 8


为什么最后一个号码错了?

感谢您的关注!

解决方案

./ 153

1 2 3 5 7 8 10 11 12 23 45 56 8


为什么最后一个号码错了?

感谢您的关注!


Mik0b0说:


大家好,

我的问题是:有两个单维数组,更长[M]和

更短[N],每个数组按升序排列。我们需要

从两个中构建一个新数组。这就是我写的:

#include< stdio.h>

#define M 8

#define N 5

main()

{

int big [M] = {1,2,5,8,10,23,45,56};

int small [N] = {3,7,11,12,100};

int new [M + N];

int i = 0, k = 0,l = 0;


while(l<(N + M))

{

if(small [k]< big [i])



你不检查以确保k< N或者i< M.一旦k == N,你需要

停止加载''small''并复制剩下的''big'',除非i == M.一旦我

== M,你需要停止加载''big'',并复制剩下的''small'',

除非k == N.


解决这个问题,我认为你的问题会消失。

-

理查德希思菲尔德

" Usenet is一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。


Mik0b0写道:


更短[N],每个数组按升序排列。我们需要

从两个中构建一个新数组。这就是我写的:

#include< stdio.h>

#define M 8

#define N 5

main()



int main(无效)


< snip code>


>

输出结果为:


[Mike @ localhost演习]

Hallo everybody,
my problem is: there are two single-dimension arrays, longer[M] and
shorter[N], every array is organized in ascending order. We need to
build a new array out of two. This is what I wrote:
#include<stdio.h>
#define M 8
#define N 5
main()
{
int big[M]={1,2,5,8,10,23,45,56};
int small[N]={3,7,11,12,100};
int new[M+N];
int i=0,k=0,l=0;

while(l<(N+M))
{
if(small[k]<big[i])
{
new[l]=small[k];
k++;
l++;
}
else
{
new[l]=big[i];
i++;
l++;
}
}

for(l=0;l<(M+N);l++)
printf("%d ",new[l]);
printf("\n");
}

The output is:

[Mike@localhost drills]$ ./153
1 2 3 5 7 8 10 11 12 23 45 56 8

Why is the last number wrong?
Thanks for your attention!

解决方案

./153
1 2 3 5 7 8 10 11 12 23 45 56 8

Why is the last number wrong?
Thanks for your attention!


Mik0b0 said:

Hallo everybody,
my problem is: there are two single-dimension arrays, longer[M] and
shorter[N], every array is organized in ascending order. We need to
build a new array out of two. This is what I wrote:
#include<stdio.h>
#define M 8
#define N 5
main()
{
int big[M]={1,2,5,8,10,23,45,56};
int small[N]={3,7,11,12,100};
int new[M+N];
int i=0,k=0,l=0;

while(l<(N+M))
{
if(small[k]<big[i])

You don''t check to ensure that k < N or that i < M. Once k == N, you need to
stop loading from ''small'' and copy the rest of ''big'', unless i == M. Once i
== M, you need to stop loading from ''big'', and copy the rest of ''small'',
unless k == N.

Fix that, and I reckon your problem will vanish.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Mik0b0 wrote:

Hallo everybody,
my problem is: there are two single-dimension arrays, longer[M] and
shorter[N], every array is organized in ascending order. We need to
build a new array out of two. This is what I wrote:
#include<stdio.h>
#define M 8
#define N 5
main()

int main(void)

<snip code>

>
The output is:

[Mike@localhost drills]


这篇关于两个较短阵列中的较长阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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