国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

2個有序數組,找出相同元素

2019-11-06 06:28:04
字體:
來源:轉載
供稿:網友
#include<iostream>#include<vector>using namespace std;//2個有序數組,a1,a2,...an,b1,b2,...bm,都是從小到大的順序排列。要求在最短時間內,找出其中相同元素。void FindCommon(const vector<int>& a, const vector<int>& b, vector<int>& c){ int posA = 0; int posB = 0; while (posA < a.size() && posB < b.size() ) { if (a[posA] > b[posB]) posB++; else if (a[posA] < b[posB]) posA++; else { //c.insert(c.end(), a[posA]); //在任意地方插入值 c.push_back(a[posA]);//在末尾插入值 posA++; posB++; } } for(int i = 0; i < c.size(); i++) {cout << c[i];}}/*void FindCommon(const vector<int>& a, const vector<int>& b, vector<int>& c) { int sa = a.size(); int sb = b.size(); int i = 0, j = 0; int cnt = 0; while(i < sa && j < sb) { if(a[i] == b[j]) { cnt++; if(cnt == 1) c.push_back(a[i]); i++; j++; cnt=0; } else if(a[i] < b[j]) { i++; cnt = 0; } else { j++; cnt = 0; } } for(int i = 0; i < c.size(); i++) {cout << c[i];}}*/int main(){ vector<int> a, b, c; a.push_back(4); a.push_back(4); a.push_back(5); a.push_back(6); b.push_back(2); b.push_back(4); b.push_back(5); b.push_back(6); FindCommon(a, b, c); system("pause");}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泗洪县| 普兰店市| 柯坪县| 沭阳县| 顺昌县| 江西省| 徐闻县| 吴川市| 建水县| 扬中市| 德兴市| 察哈| 莱西市| 四子王旗| 易门县| 宜君县| 博乐市| 伊吾县| 肇东市| 紫阳县| 航空| 佳木斯市| 武城县| 乌什县| 绵阳市| 慈利县| 宜兰市| 奉贤区| 德格县| 温泉县| 郯城县| 上杭县| 高安市| 梅河口市| 黎川县| 祁连县| 山阳县| 友谊县| 镇巴县| 彰武县| 大冶市|