题目标题:
将两个整型数组按照升序合并,并且过滤掉重复数组元素
详细描述:
接口说明
原型:

voidCombineBySort(int* pArray1,intiArray1Num,int* pArray2,intiArray2Num,int*
pOutputArray,int* iOutputNum);
输入参数:
int* pArray1 :整型数组1

intiArray1Num:数组1元素个数
int* pArray2 :整型数组2
intiArray2Num:数组2元素个数

输出参数(指针指向的内存区域保证有效):
int* pOutputArray:合并后的数组
int*
iOutputNum:合并后数组元素个数
返回值:void

输入说明,按下列顺序输入:
1 输入第一个数组的个数
2 输入第一个数组的数值
3 输入第二个数组的个数

4.输入第二个数组的数值

样例输入:

3 1 2 5 4 -1 0 3 2
样例输出:
-101235

#include<iostream>
#include<vector>
#include <algorithm> using namespace std; void CombineBySort(int* pArray1,int iArray1Num,int* pArray2,int iArray2Num,int* pOutputArray,int* iOutputNum)
{
int i;
vector<int> array_tmp;
for(i=;i<iArray1Num;i++)
array_tmp.push_back(*(pArray1+i));
for(i=;i<iArray2Num;i++)
array_tmp.push_back(*(pArray2+i));
sort(array_tmp.begin(),array_tmp.end());
//array_tmp.erase(unique(array_tmp.begin(),array_tmp.end()));
vector<int>::iterator it=array_tmp.begin();
vector<int> out;
int tmp=*it;
out.push_back(tmp);
while(it!=array_tmp.end())
{
if(tmp!=*it)
{
tmp=*it;
out.push_back(tmp);
}
it++;
} *iOutputNum=out.size();
for(i=;i<*iOutputNum;i++)
*(pOutputArray+i)=out[i];
} int main()
{
int Num1,Num2,i;
cin >>Num1;
int *Array1=(int*)malloc(Num1*sizeof(int));
for(i=;i<Num1;i++)
cin>>Array1[i];
cin >>Num2;
int *Array2=(int*)malloc(Num2*sizeof(int));
for(i=;i<Num2;i++)
cin>>Array2[i]; int *outputArray=(int*)malloc((Num2+Num1)*sizeof(int));
int outNum=;
CombineBySort(Array1,Num1,Array2,Num2,outputArray,&outNum); for(i=;i<outNum;i++)
cout <<outputArray[i]; free(Array1);
free(Array2);
free(outputArray);
return ;
}

  上述代码使用的是vector容器。也可以使用set容器,见如下:

  华为OJ(整形数组合并)

  vector中去除重复的元素

  在删除相同元素时,有人使用的是STL算法 unique,但其对用的测试用例不行,如 4 1 1 1 1 3 1 1 1;

其输出结果为  111111

  故在本程序中为使用unique算法。


华为oj---合并数组的更多相关文章

  1. 华为OJ平台试题 ——数组:整形数组合并

    代码: /* * 将两个整型数组依照升序合并,而且过滤掉反复数组元素 */ #include <stdio.h> #define N 256 #define M 512 /* * 合并数组 ...

  2. PHP中通过加号合并数组

    通常,我们合并多个数组用的是array_merge()函数,其实,PHP手册中关于数组操作符的介绍给了我们更简单的方法,那就是"+"号,看看下面的例子就明白了(详细了解) 代码: ...

  3. PHP合并数组array_merge函数运算符加号与的区别

    两个的区别是:1.数组键名为数字键名时,要合并的两个数组中有同名数字KEY的时候,使用array_merge()不会覆盖掉原来的值,而使用“+”合并数组则会把最先出现的值作为最终结果返回,而把后面的数 ...

  4. 华为OJ:火车进站

    火车进站 给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号.要求以字典序排序输出火车出站的序列号. 输入描述: 有多组测试用例, ...

  5. PHP合并数组保留key值

    PHP合并数组,键值不变   尝试了好几个合并数组的函数, 但是都是把key值重置, 导致key值丢失(因为key值是要用到的) 大大说, 最好用数组的相关函数, 网上随意找了下, 还是没找到. 因为 ...

  6. PHP中使用函数array_merge()合并数组

    如果明白数组其实就是map的话,我想你就会明白array_merge为什么要这么实现了 PHP中合并数组分成两种情况 1.如果这两个数组中有相同的字符串键名: <?php header('Con ...

  7. PHP合并数组+与array_merge的区别分析

    主要区别是两个或者多个数组中如果出现相同键名,键名分为字符串或者数字,需要注意 1)键名为数字时,array_merge()不会覆盖掉原来的值,但+合并数组则会把最先出现的值作为最终结果返回,而把后面 ...

  8. PHP 合并数组 追加数组例子

    PHP合并数组我们可以使用array_merge()函数,array_merge()函数返回一个联合的数组.所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次追加.其形式为: arra ...

  9. 在华为oj的两个月

    一次偶然的机会,我接触到华为oj平台(http://career-oj.huawei.com/exam/camLogin.jsp),当时的心情很是兴奋,于是立马注册开通,然后迫不及待地上去做题.刚开始 ...

随机推荐

  1. MySql绿色版安装过程记录

    作为程序猿,要多动手,周末趁着有空且笔记本刚刚装了系统,所以就配置了下绿色版的MySQL. 多动手,多动手,多动手. 多总结,多总结,多总结. 以下为正文: 一.下载MySQL绿色版: 1.这个地址: ...

  2. 轻量级sqlite是增删改查

    --创建数据库 create database ios --使用数据库 use ios --创建数据表 create table student ( stuid int primary key aut ...

  3. Bootstrap学习指南

    一.Bootstrap简介 二.Bootstrap安装 三.Bootstrap CSS 四.Bootstrap 布局组件 五.Bootstrap 插件 六.Bootstrap定制

  4. Bar Chart of Frequency of modals in different sections of the Brown Corpus

    Natural Language Processing with Python Chapter 4.8 colors = 'rgbcmyk' # red, green, blue, cyan, mag ...

  5. UVa11555 - Aspen Avenue

    今晚CF GYM A题,神坑.. 原题: Aspen Avenue ``Phew, that was the last one!'' exclaimed the garden helper Tim a ...

  6. FZU 1056 扫雷游戏

    水题.统计一下周围有几个雷. #include<cstdio> #include<cstring> #include<cmath> #include<algo ...

  7. DP题目列表/弟屁专题

    声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 10 ...

  8. IOS block 对象强引用和若引用

    1. 在block外面这样:__weak MyController *weakSelf = self 或者 __weak __typeof(self) weakSelf = self;是为了防止强引用 ...

  9. 在IOS中使用DES算法对Sqlite数据库进行内容加密存储并读取解密

    在IOS中使用DES算法对Sqlite 数据库进行内容加密存储并读取解密 涉及知识点: 1.DES加密算法: 2.OC对Sqlite数据库的读写: 3.IOS APP文件存储的两种方式及读取方式. 以 ...

  10. eclipse hibernate plugin

    JBoss Tools hibernate tools for eclipse plugins