#include <iostream>
#include<map>
using namespace std;
int ansindex = 0;
pair<int, int>ans[1000];
int main()
{
map<int, int>maps;
int row, col;
cin >> row >> col;
int times = 0;
cin >> times;
for (int i = 0; i < times; i++) {
int x, y, z;
cin >> x >> y >> z;
int index = x * col + y + 1;
int value = z;
if (maps.find(index) == maps.end()) {
maps.insert(pair<int, int>(index, value));
}
else {
maps[index] = maps[index] + value;
}
}
cin >> times;
for (int i = 0; i < times; i++) {
int x, y, z;
cin >> x >> y >> z;
int index = x * col + y + 1;
int value = z;
if (maps.find(index) == maps.end()) {
maps.insert(pair<int, int>(index, value));
}
else {
maps[index] = maps[index] + value;
}
}
std::map<int, int>::iterator it = maps.begin();
for (; it != maps.end();)
{
if (it->second == 0)
{
maps.erase(it++);
}
else {
it++;
}
}
if (maps.size() == 0) {
cout << maps.size();
}
else {
cout << maps.size() << endl;
it = maps.begin();
if (!maps.empty()) {
while (it != maps.end()) {
ans[ansindex].first = it->first;
ans[ansindex++].second = it->second;
it++;
}
}
for (int i = 0; i < ansindex - 1; i++) {
int nowrow = 0;
while (ans[i].first > col) {
ans[i].first -= col;
nowrow++;
}
int nowcol = ans[i].first-1;
cout << nowrow << " " << nowcol << " " << ans[i].second << endl;
}
if (ansindex != 0) {
int nowrow = 0;
while (ans[ansindex - 1].first > col) {
ans[ansindex-1].first -= col;
nowrow++;
}
int nowcol = ans[ansindex - 1].first-1;
cout << nowrow << " " << nowcol << " " << ans[ansindex - 1].second;
}
}
system("pause");
return 0;
}
稀疏矩阵加法(PTA)使用map为什么不对中间两个测试的怎么都过不了?
于 2023-12-20 15:26:00 首次发布