【算法竞赛宝典】稀疏数组

【算法竞赛宝典】稀疏数组

  • 题目描述
  • 代码展示

题目描述


代码展示

random.cpp

#include 

using namespace std;

int main() {
    freopen("zip5.in", "w", stdout);
    int n, m;
    cin >> n >> m;
    cout << n << ' ' << m << endl;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++)
            cout << 0 << ' ';
        cout << endl;
    }
}

zip.cpp

//压缩数组
#include 

using namespace std;

int main() {
    freopen("zip.in", "r", stdin);
    freopen("zip.out", "w", stdout);
    int N, M, temp, num = 0, index = 1;
    cin >> N >> M;
    int zip[N * M / 3][3];//有效数据不会多于全部元素的三分之一

    for (int i = 0; i < N; ++i)
        for (int j = 0; j < M; ++j) {
            cin >> temp;
            if (temp != 0) {
                num++;
                zip[index][0] = i;
                zip[index][1] = j;
                zip[index][2] = temp;
                index++;
            }
        }
    zip[0][0] = N;
    zip[0][1] = M;
    zip[0][2] = num;

    for (int i = 0; i <= num; i++)
        cout << zip[i][0] << " " << zip[i][1] << " " << zip[i][2] << endl;
    return 0;
}

你可能感兴趣的:(C++刷题,算法竞赛宝典,算法,C++)