题目描述
建立顺序表的结构体,属性包括:数组、实际长度、最大长度(设定为1000)
已知两个递增序列,把两个序列的数据合并到顺序表中,并使得顺序表的数据递增有序
输入
第1行先输入n表示有n个数据,接着输入n个数据,表示第1个序列,要求数据递增互不等
第2行先输入m表示有m个数据,接着输入m个数据,表示第2个序列,要求数据递增互不等
输出
顺序表内容包括顺序表的实际长度和数据,数据之间用空格隔开
第1行输出创建后的顺序表内容
输入输出样例
输入样例1 <-复制
3 11 33 55
5 22 44 66 88 99
输出样例1
8 11 22 33 44 55 66 88 99
AC代码
#include<iostream>
using namespace std;
struct seqlist
{
int* list;
int size, max=1000;
};
seqlist hebing(seqlist a, seqlist b)
{
seqlist c;
c.size = a.size + b.size;
c.list = new int[c.size];
int i, j,k;
i = j = k=0;
while ((i < a.size) && (j < b.size))
{
if (a.list[i] < b.list[j])
c.list[k++] = a.list[i++];
else
c.list[k++] = b.list[j++];
}
while (i < a.size)
{
c.list[k++] = a.list[i++];
}
while (j < b.size)
{
c.list[k++] = b.list[j++];
}
return c;
}
void show(seqlist c)
{
int i;
cout << c.size << " ";
for (i = 0; i < c.size; i++)
cout << c.list[i] << " ";
cout << endl;
}
int main()
{
int n;
cin >> n;
seqlist biao1;
biao1.size = n;
biao1.list = new int[n];
int i;
for (i = 0; i < n; i++)
cin >> biao1.list[i];
int m;
cin >> m;
seqlist biao2;
biao2.size = m;
biao2.list = new int[m];
for (i = 0; i < m; i++)
cin >> biao2.list[i];
seqlist c = hebing(biao1, biao2);
show(c);
}
(by 归忆)