B.合并数列(SB题)
B.合并数列 | |||||
| |||||
Description | |||||
给定两个长度分别为n和m(n和m的长度不大于10000)的非递减整数数列a(a0,a1,a2,...,an-1)和b(b0,b1,b2,...,bm-1),数列中各个整数的取值范围[0,100)。 现在对这两个数列进行合并得到新数列c(c0,c1,c2,...,cn+m-1),并保证数列c仍然是非递减的。 输出合并后数列c的各项,并把该数列c中出现次数最多的最小整数及其出现的次数输出。 | |||||
Input | |||||
有一组测试数据。 第一行输入的是两个数列的长度n和m,数据之间由空格分隔。 第二行输入的是长度为n的数列的各项,数据之间由空格分隔。 第三行输入的是长度为m的数列的各项,数据之间由空格分隔。 | |||||
Output | |||||
第一行输出合并后的数列的各项,数据之间由空格分隔。 第二行按照样例的格式输出合并后数列中出现次数最多的最小整数及其出现的次数。 | |||||
Sample Input | |||||
10 5 0 3 5 7 9 11 11 98 98 99 0 0 4 6 11 99 | |||||
Sample Output | |||||
0 0 0 3 4 5 6 7 9 11 11 11 98 98 99 0 appears 3 times. | |||||
Hint | |||||
使用给定的主函数main()。不使用该主函数或修改该主函数,本题不得分。 int main() { int n,m,ans,totalmax=0; cin>>n>>m; int *array1=new int[n]; int *array2=new int[m]; for(int i=0;i<n;i++)cin>>array1[i]; for(int i=0;i<m;i++)cin>>array2[i]; ans=merge(array1,n,array2,m,totalmax);//实现该函数 cout<<ans<<" appears "<<totalmax<<" times.\n"; return 0; } |
#include<iostream>
#include<algorithm>
using namespace std;
int merge(int array1[], int n, int array2[], int m, int& tota)
{
int* arr = new int[n + m + 1];
for (int i = 0; i < n; i++)
{
arr[i] = array1[i];
}
for (int i = n; i < n + m; i++)
{
arr[i] = array2[i - n];
}
sort(arr, arr + n + m);
int* qwe = new int[n + m + 1];
int k = 1;
for (int i = 0; i < n + m; i++)
{
cout << arr[i] << ' ';
qwe[i] = k;
if (arr[i] == arr[i + 1])k++;
else k = 1;
}
cout << endl;
int ma = 0;
int asd = 0;
int ff = 0;
for (int i = 0; i < n + m; i++)
{
if (qwe[i] > ma)
{
asd = qwe[i];
ff = arr[i];
ma = qwe[i];
}
}
tota = asd;
return ff;
}
int main()
{
int n, m, ans, totalmax = 0;
cin >> n >> m;
int* array1 = new int[n];
int* array2 = new int[m];
for (int i = 0; i < n; i++)cin >> array1[i];
for (int i = 0; i < m; i++)cin >> array2[i];
ans = merge(array1, n, array2, m, totalmax);//实现该函数
cout << ans << " appears " << totalmax << " times.\n";
return 0;
}