假设我有两个代表数字波形图的点(x,y)
列表,我可以从左到右迭代,总是在y = 0
上开始和结束。
例如:
Graph1 = [(0,0) (0,3) (4,3) (4,0) (9,0) (9,1) (11,1) (11,3) (15,3) (15,2) (17,2) (17,4) (19,4) (19,0)]
Graph2 = [(0,0) (0,1) (6,1) (6,0) (9,0) (9,1) (13,1) (13,0) (16,0) (16,1) (18,1) (18,0) (19,0)]
算法的伪代码如何,或者算法的名称是什么,它会在返回以下列表的y
值上添加这两个波形图:
Result = [(0,0) (0,4) (4,4) (4,1) (6,1) (6,0) (9,0) (9,2) (11,2) (11,4) (13,4) (13,3) (15,3) (15,2) (16,2) (16,3) (17,3) (17,5) (18,5) (18,4) (19,4) (19,0)]
答案 0 :(得分:0)
我带了一段时间,但也许是这样的?
list<Point*>* addProfiles(list<Point*>* pl1, list<Point*>* pl2) {
if(pl1->empty()) return pl2;
if(pl2->empty()) return pl1;
list<Point*>* res = new list<Point*>;
int i = 0, j = 0;
while(i < pl1->size() && j < pl2->size()) {
Point* p1 = pl1->at(i);
Point* p2 = pl2->at(j);
if(p1->x() < p2->x()) {
res->append(new Point(p1->x(), p1->y()+p2->y()));
i++;
}
else if(p1->x() > p2->x()) {
res->append(new Point(p2->x(), p1->y()+p2->y()));
j++;
}
else {
res->append(new Point(p1->x(),p1->y()+p2->y()));
i++;
j++;
}
}
while(i < pl1->size()) {
res->append(new Point(pl1->at(i)->x(), pl1->at(i)->y()+pl2->last()->y()));
i++;
}
while(j < pl2->size()) {
res->append(new Point(pl2->at(j)->x(), pl1->last()->y()+pl2->at(j)->y()));
j++;
}
return res;
}