【新手求教】C++合并数组的??

C++语言 码拜 9年前 (2015-05-11) 1018次浏览 0个评论
 

#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int main()
{
int m,n;
int a[100],b[100],c[100];
while(cin>>m)
{
for(int i = 0;i < m;i++)
cin >> a[i];
}
while(cin >> n)
{
int t = m;
for(int j = 0;j < n;j++)
{
cin >> b[j];
c[t] = b[j];
t++;
}
}
         sort(c[0],c[m+n-1]);
for(int i = 0;i < m+n;i++)
   cout<<c[i]<<” “;
cout<<endl;
}
题目要求合并数组并排序!不去重复的。。
不能运行,提示:两个重载中没有一个可以转换成所有参数的
要怎么解决啊????
求帮助,,谢谢各位!

10分
参考链表的二路合并算法
https://github.com/707wk/Senior-middle-school/blob/master/Filling%20in%20the%20gaps.c
好复杂,链表我太懂得,,而且C我好多都看不懂的‘’
sort函数的参数是地址,你的参数搞错了.
15分
像这样才行.
【新手求教】C++合并数组的??
sort的参数是iterator,在这里应该是地址,比如c,c+m之类,这里a用来做什么的
15分
这样可好:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int main()
{
	int i, j, r;
	int m, n;
	int a[100], b[100], c[100];
	cin >> m;
	for (i = 0; i < m; i++) cin >> a[i];
	sort(a, a + m);
	cin >> n;
	for (i = 0; i < n; i++) cin >> b[i];
	sort(b, b + n);
	i = j = r = 0;
	while (i < m && j < n)
	{
		if (a[i] <= b[j]) c[r++] = a[i++];
		else c[r++] = b[j++];
	}
	while (i < m) c[r++] = a[i++];
	while (j < n) c[r++] = b[j++];
	for (int i = 0; i < m + n; i++) cout << c[i] << " ";
	cout << endl;
	return 0;
}
//3
//2 1 5
//4
//23 45 1 25
//1 1 2 5 23 25 45
懂了,,谢谢

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明【新手求教】C++合并数组的??
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!