堆棧隊列和數組-行邏輯連結疏鬆陣列

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   for   sp   div   on   

#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* transpose(RLSMatrix*  src){    RLSMatrix* ans =createRLSMatrix(src->c,src->r,src->cnt);    int* ccnts = new int[src->c+1];    int* cpos = new int[src->c+1];    cpos[0]=0;    for(int i=0;i<=src->c;i++)        ccnts[i]=0;    for(int i=0;i<src->cnt;i++)        ccnts[src->tripples[i].y]++;    for(int i=1;i<=src->c;i++)    {        ans->rpos[i]=ans->rpos[i-1]+ccnts[i-1];        cpos[i] = ans->rpos[i];    }    delete[] ccnts;    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);//彰顯非0元                cout<<src->tripples[cur].value<<"   ";                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//設定三色相加                cur++;            }            else cout<<"0   ";        }        cout<<endl;    }}void main(){     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//設定三色相加     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;}

 

堆棧隊列和數組-行邏輯連結疏鬆陣列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.