标签:style blog color io os for sp div on
#include<iostream> #include <iomanip> #include"windows.h" using namespace std; struct OLinMatrixkNode { int data; int x,y; OLinMatrixkNode* right; OLinMatrixkNode* down; }; struct OLinMatrix { OLinMatrixkNode** RowHead; OLinMatrixkNode** ColHead; int r,c,cnt; }; OLinMatrix* createOLinMatrix(int r,int c) { OLinMatrix* ans=(OLinMatrix*)malloc(sizeof(OLinMatrix)); ans->RowHead=new OLinMatrixkNode*[r+1]; ans->ColHead=new OLinMatrixkNode*[c+1]; for(int i=1;i<=r;i++) ans->RowHead[i]=NULL; for(int i=1;i<=c;i++) ans->ColHead[i]=NULL; ans->cnt=0; ans->r=r; ans->c=c; return ans; } OLinMatrixkNode* createOLinMatrixNonde(int r,int c,int v) { OLinMatrixkNode* ans=(OLinMatrixkNode*)malloc(sizeof(OLinMatrixkNode)); ans->x=r; ans->y=c; ans->data=v; return ans; } void addElement(OLinMatrix* p,int r,int c,int v) { if(v==0) return; OLinMatrixkNode* newNode = createOLinMatrixNonde(r,c,v); if(p->RowHead[r]==NULL || p->RowHead[r]->y > c) { newNode->right=p->RowHead[r]; p->RowHead[r]=newNode; } else { OLinMatrixkNode* temp = p->RowHead[r]; while( temp->right!=NULL && temp->right->y <= c) temp=temp->right; newNode->right=temp->right; temp->right=newNode; if(p->ColHead[c]==NULL || p->ColHead[c]->x > r) { newNode->down=p->ColHead[c]; p->ColHead[c]=newNode; } else { OLinMatrixkNode* temp = p->ColHead[c]; while( temp->down!=NULL && temp->down->x <= c) temp=temp->down; newNode->down=temp->down; temp->down=newNode; } } p->cnt++; } void output(OLinMatrix* p) { for(int i=1;i<=p->r;i++) { OLinMatrixkNode* temp = p->RowHead[i]; for(int j=1;j<=p->c;j++) { if(temp!=NULL && temp->y==j) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);//彰显非0元 cout<<std::left<<setw(3)<<temp->data; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//设置三色相加 temp=temp->right; } else { cout<<std::left<<setw(3)<<0; } } cout<<endl; } } void main() { int r=7,c=12,cnt=15; OLinMatrix* m= createOLinMatrix(r,c); bool v[100][100]; memset(v,false,sizeof(100)); for(int i=1;i<=cnt;i++) { int x=rand()%r+1,y=rand()%c+1,value=rand()%99+1; if(v[x][y]) { addElement(m,x,y,value); cout<<x<<" "<<y<<" "<<value<<endl; v[x][y]=0; } else { i--; } } output(m); cin>>r; }
标签:style blog color io os for sp div on
原文地址:http://www.cnblogs.com/kbyd/p/3998622.html