本来想用 vector ,试了下不好实现,换用 node 的数组
重载运算
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
const int maxn = 10000;
struct node {
int r, c, v;
};
bool cmp(node aa, node bb) {
if(aa.r == bb.r) return aa.c < bb.c;
return aa.r < bb.r;
}
class Triple
{
private:
node a_[maxn];
int len;
public:
Triple() {}
~Triple() {}
friend istream &operator >> (istream &in, Triple &t) {
int i = 0;
while(1) {
in >> t.a_[i].r >> t.a_[i].c >> t.a_[i].v;
if(t.a_[i].r == 0 && t.a_[i].c == 0) break;
++i;
}
t.len = i;
return in;
}
friend ostream &operator << (ostream &out, Triple& t) {
sort(t.a_, t.a_+t.len, cmp);
//cout << "++++" << endl;
for(int i = 0; i < t.len; i++) {
if(t.a_[i].v != 0)
out << t.a_[i].r << " " << t.a_[i].c << " " << t.a_[i].v << endl;
}
//cout << "++++" << endl;
return out;
}
Triple operator + (Triple &b) {
for(int i = 0; i < b.len; ++i)
a_[i+len] = b.a_[i];
len = len + b.len;
sort(a_, a_+len, cmp);
for(int i = 0; i < len-1; ++i)
if(a_[i].r == a_[i+1].r && a_[i].c == a_[i+1].c) {
a_[i].v += a_[i+1].v;
a_[i+1].v = 0;
}
return *this;
}
};
int main()
{
Triple mat1, mat2, mat3;
cin>>mat1;
cin>>mat2;
mat3 = mat1 + mat2;
cout<<mat3;
return 0;
}