Stack queue and array-triple Sparse Matrix

Source: Internet
Author: User
# Include <iostream> # include <iomanip> # include "windows. H "using namespace STD; struct tripple {int X, Y, value ;}; struct tripplematrix {int R, C, CNT; tripple * tripples ;}; tripplematrix * createtripplematrix (int r, int C, int maxcnt) {tripplematrix * P = (tripplematrix *) malloc (sizeof (tripplematrix); P-> r = R; p-> C = C; P-> CNT = 0; P-> tripples = new tripple [maxcnt]; return P;} tripplematrix * transpose (tripplematrix * SRC) {tripplematrix * ans = createtripplematrix (SRC-> C, Src-> r, Src-> CNT); For (INT I = 1; I <= ANS-> r; I ++) {for (Int J = 0; j <Src-> CNT; j ++) {If (SRC-> tripples [J]. y = I) {tripple newtripple; newtripple. X = I; newtripple. y = Src-> tripples [J]. x; newtripple. value = Src-> tripples [J]. value; ANS-> tripples [ANS-> CNT ++] = newtripple ;}}return ans;} void output (tripplematrix * 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 yuan 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 = 12, c = 17, maxcnt = 40; tripplematrix * m = createtripplematrix (R, C, maxcnt); 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; m-> CNT ++ ;}} output (m); cout <Endl; output (transpose (m )); cin> r ;}

 

Stack queue and array-triple Sparse Matrix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.