# Include <iostream> # include <iomanip> # include "windows. H "using namespace STD; struct tripple {int X, Y, value ;}; struct rlsmatrix {int R, C, CNT; tripple * tripples; int * rpos ;}; rlsmatrix * createrlsmatrix (int r, int C, int maxcnt) {rlsmatrix * P = (rlsmatrix *) malloc (sizeof (rlsmatrix); P-> r = R; p-> C = C; P-> CNT = 0; P-> tripples = new tripple [maxcnt]; p-> rpos = new int [p-> r + 1]; P-> rpos [0] = 0; return P;} rlsmatrix * transpo Se (rlsmatrix * SRC) {rlsmatrix * ans = createrlsmatrix (SRC-> C, Src-> r, Src-> CNT ); int * ccnt = new int [Src-> C + 1]; int * CPOs = new int [Src-> C + 1]; CPOs [0] = 0; for (INT I = 0; I <= Src-> C; I ++) ccnt [I] = 0; For (INT I = 0; I <Src-> CNT; I ++) ccnt [Src-> tripples [I]. y] ++; For (INT I = 1; I <= Src-> C; I ++) {ANS-> rpos [I] = ANS-> rpos [I-1] + ccnt [I-1]; CPOs [I] = ANS-> rpos [I];} delete [] ccnt; tripple newtripple; For (INT I = 0; I <Src-> CNT; I ++) {newtripple. X = Src-> tripples [I]. y; newtripple. y = Src-> tripples [I]. x; newtripple. value = Src-> tripples [I]. value; ANS-> tripples [CPOs [newtripple. x] ++] = newtripple; ANS-> CNT ++;} return ans;} void output (rlsmatrix * SRC) {int cur = 0; For (INT I = 1; I <= Src-> r; I ++) {for (Int J = 1; j <= Src-> C; j ++) {If (SRC-> tripples [cur]. X = I & Src-> tripples [cur]. y = J) {setconsoletextattribute (getstdhandle (std_output_handle ), Foreground_intensity | foreground_green); // displays non-0 RMB cout <Src-> tripples [cur]. value <"; setconsoletextattribute (getstdhandle (std_output_handle), foreground_intensity | foreground_red | foreground_green | foreground_blue); // set three colors to add cur ++ ;} else cout <"0" ;}cout <Endl ;}void main () {setconsoletextattribute (getstdhandle (handle), role | foreground_red | foreground_green | foreground_blue ); // Set the three-color addition int r = 11, c = 18, maxcnt = 45; rlsmatrix * m = createrlsmatrix (R, C, maxcnt); int lstr = 0; for (INT I = 1; I <= r; I ++) {for (Int J = 1; j <= C; j ++) {If (RAND () % 10 = 1 & M-> CNT <maxcnt) {M-> tripples [M-> CNT]. X = I; m-> tripples [M-> CNT]. y = J; m-> tripples [M-> CNT]. value = rand () % 10; if (I! = Lstr) m-> rpos [I] = m-> CNT; m-> CNT ++;} lstr = I;} output (m); cout <Endl; output (transpose (m); CIN> r ;}
Stack queue and array-row Logical Link Sparse Matrix