您的位置:首页 > 其它

La_Lb_Lc

2012-10-08 09:44 183 查看
有数据类型为整型的顺序表La和Lb,其数据元素均按从小到大的升序排列,编写一个算法将它们合并成一个表Lc,要求Lc中数据元素也按升序排数据列。

用数组实现的代码如下:

static void Main(string[] args)
{
int[] La = { 1, 3, 5, 7, 9 };
int[] Lb = { 2, 4, 6, 8, 10 };
int[] Lc = new int[La.Length + Lb.Length];
int i = 0;
int j = 0;
int k = 0;
while ((i <= La.Length - 1) && (j <= Lb.Length - 1))
{
if (La[i] <= Lb[j])
{
Lc[k++] = La[i++];
}
else
{
Lc[k++] = Lb[j++];
}
}

//a表中还有数据元素
while (i <= La.Length - 1)
{
Lc[k++] = La[i++];
}
//b表中还有数据
while (j <= Lb.Length - 1)
{
Lc[k++] = Lb[j++];
}
foreach (int c in Lc)
{
Console.Write(c + " ");
}

Console.ReadKey();
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐