//用两个数组,两个索引变量
#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<string.h>
#include<stdio.h>
#include<iomanip>
#include<stdlib.h>
#include<algorithm>
#include<cmath>
using namespace std;
template <typename T>
int merge_(T A[], T B[], T C[], int n, int m){
int i=0, j=0, index=0;
while(i<n && j<m){
if(A[i] < B[j]){
C[index++] = A[i++];
}
else{
C[index++] = B[j++];
}
}
while(i<n) C[index++] = A[i++];
while(j<m) C[index++] = B[j++];
return index;
}
int main(){
int A[] = {1,3,5,7,9};
int B[] = {2,4,6,8,10};
int C[12] = {0};
int res = merge_(A, B, C, 5, 5);
//cout<<res<<endl;
for(int i=0;i<10;i++){
cout<<C[i]<< " ";
}
return 0;
}
[算法]序列合并模板
最新推荐文章于 2020-11-01 17:17:06 发布