DS顺序表--合并操作 C++

目录

题目描述

思路分析

AC代码


题目描述

建立顺序表的结构体,属性包括:数组、实际长度、最大长度(设定为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

思路分析

整一个结构体,然后函数写成全局函数。

搞那么复杂干什么,全都存到一个顺序表里面,然后用sort排序一下不就完了。

AC代码

#include <bits/stdc++.h>
using namespace std;
struct SeqList {
	int *List = NULL;
	int size, maxsize = 1000;
};
void CreatList(SeqList&test) {
	cin >> test.size;
	test.List = new int[test.size];
	for (int i = 0; i < test.size; i++)
		cin >> test.List[i];
}
void print(SeqList&temp) {
	cout << temp.size << ' ';
	for (int i = 0; i < temp.size; i++)
		cout << temp.List[i] << ' ';
	cout << endl;
}
int main() {
	SeqList a, b, c;
	CreatList(a);
	CreatList(b);
	c.size = a.size + b.size;
	c.List = new int[c.size];
	for (int i = 0; i < a.size; i++)
		c.List[i] = a.List[i];
	for (int i = 0; i < b.size; i++)
		c.List[i + a.size] = b.List[i];
	sort(c.List, c.List + c.size);
	print(c);
}

猜你喜欢

转载自blog.csdn.net/weixin_62264287/article/details/126944783