c语言的问题 大家帮帮我

来源:百度知道 编辑:UC知道 时间:2024/06/17 23:09:07
编写一个合并程序,完成对两个有序序列进行归并排序,仍按从大到小,相同的只取一个,函数调用形式:
merger(arr1,n1,arr2,n2)
函数返回归并后的总长度。主函数完成对两个数列的赋值、对归并后的结果输出
这么多分都没人回答吗

我来试着回答.程序在VC中运行.
#define MAXSIZE 50
#include<iostream.h>

template<class T1>
int merger(T1 arr1[],int n1,T1 arr2[],int n2)
{
int result[MAXSIZE];//保存临时排序数组的结果.
int count=0,a=0,b=0;

while(a<n1 && b<n2)
{
if(arr1[a]>arr2[b])
result[count++]=arr1[a++];
else if(arr1[a]==arr2[b])
{
result[count++]=arr1[a++];
b++;
}
else
result[count++]=arr2[b++];
}

while(a<n1)
{
result[count++]=arr1[a++];
}

while(b<n2)
{
result[count++]=arr2[b++];
}

for(int i=0;i<count;i++)//把排序结果保存在第一个数组中.
arr1[i]=result[i];
return count;
}

int main()
{
int arr1[MAXSIZE/2]={99,97,86,75,65,54,44,33,22,12};
int arr2[MAXSIZE/2]={87,67,65,54,43,33,32,31,23,11};//测试用的数据

int n=merger(arr1,10,arr2,10);
for(int i=