#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");}