题目:设计一个程序,生成两个按值非递减有序排列的线性表LA和LB,再将LA和LB归并为一个新的线性表LC,且LC中的数据仍按值非递减有序排列,输出线性表LA、LB、LC。
以下是自己的代码,但是好像是main 函数的后半部分从设置完LA和LB的随机数之后就有错误,不知道错在哪,
##
#include <stdio.h>
#include <algorithm>
#include<time.h>
#include<cstdlib>
#include<iostream>
using namespace std;
bool cmp(int a,int b)
{
return a <= b;
}
void print_array(int a[],int range)
{
int i = 0;
while(i < range)
{
printf("%d ",a[i ++]);
}
printf("\n");
}
int main()
{
int T1, T2;
srand((unsigned)time(NULL));
cout << "请输入第一个线性表元素的个数:" << endl;
cin >> T1;
int* LA= new int[T1];
for (int i = 0; i < T1; i++) {
LA[i] = rand()%10;
}
cout << "请输入第二个线性表元素的个数:" << endl;
cin >> T2;
int* LB = new int[T2];
for (int i = 0; i < T2; i++)
{
LB[i] = rand()%10;;
}
int LC[T1+T2];
sort(LA,LA + T1,cmp);
printf("LA: ");
print_array(LA,T1);
sort(LB,LB + T2,cmp);
printf("LB: ");
print_array(LB,T2);
int i = 0;
while(i < T2)
{
LC[i] = LA[i];
LC[i + T2] = LB[i ++];
}
sort(LC,LC + T1+T2,cmp);
printf("LC: ");
print_array(LC,T1+T2);
system("pause");
return 0;
}