[数据结构-严蔚敏版]P95矩阵压缩-特殊矩阵的存储(对称矩阵,三角矩阵)
生活随笔
收集整理的這篇文章主要介紹了
[数据结构-严蔚敏版]P95矩阵压缩-特殊矩阵的存储(对称矩阵,三角矩阵)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
對稱矩陣的存儲:
代碼如下:
#include <iostream> using namespace std;int main() {int n;cin >> n;int *a;a = new int[(n*(n + 1)) / 2];for (int i = 0; i < (n*(n + 1)) / 2; i++){cin >> a[i];}for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){if (i >= j){int idx = (i*(i - 1)) / 2 + (j - 1);cout << a[idx] << " ";}else{int idx = (j*(j - 1)) / 2 + (i - 1);cout << a[idx] << " ";}}cout << endl;}return 0; }三角矩陣的存儲:
代碼如下:
#include <iostream> using namespace std;int main() {int *a;int n;cin >> n;int N = ((n*(n + 1)) / 2) + 1;a = new int[N];for (int i = 0; i < N; i++){cin >> a[i];}for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){if (i >= j){int idx = (i*(i - 1)) / 2 + (j - 1);cout << a[idx] << " ";}else{cout << a[N - 1] << " ";}}cout << endl;}return 0; }總結
以上是生活随笔為你收集整理的[数据结构-严蔚敏版]P95矩阵压缩-特殊矩阵的存储(对称矩阵,三角矩阵)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小明推出 Q3 Neo 投影仪:1080
- 下一篇: 快手 2023 年 Q3 营收 279.