首页 > 代码库 > 队列堆栈和数组-三角矩阵
队列堆栈和数组-三角矩阵
#include<iostream>#include <iomanip>using namespace std;enum TriangleTye{leftBottom,leftUp,rightBottom,rightUp};struct TriangleMatrix{ int* nums; int scale; TriangleTye triangleTye; int size;};TriangleMatrix* createTriangleMatrix(int scale,TriangleTye triangleTye){ int size = scale*(scale+1)/2; TriangleMatrix* p = (TriangleMatrix*)malloc(sizeof(TriangleMatrix)); p->nums = new int[size+1]; p->scale=scale; p->triangleTye=triangleTye; p->size=size; return p;}int getElement(TriangleMatrix* matrix,int i,int j){ switch ((matrix->triangleTye)) { case leftBottom: if(i<j) return 0; else return matrix->nums[i*(i-1)/2+j-1]; break; case leftUp: if(matrix->scale-i+1<j) return 0; else return matrix->nums[matrix->size - ((matrix->scale-i)*(matrix->scale-i+1)/2+(matrix->scale-i+1-j))-1]; break; case rightBottom: if(matrix->scale-i+1>j) return 0; else return matrix->nums[i*(i-1)/2+j-(matrix->scale-i)-1]; break; case rightUp: if(i>j) return 0; else return matrix->nums[matrix->size - (matrix->scale-i+1)*(matrix->scale-i+2)/2+j-i]; break; default: return -1; break; }}void OuptPut(TriangleMatrix* p){ for(int i=1;i<=p->scale;i++) { for(int j=1;j<=p->scale;j++) { printf("%d ",getElement(p,i,j)); } printf("\n"); }}void main(){ printf("\n"); printf("%s","four kinds of triangle matrix, and the elements is:"); printf("\n"); int x=9; TriangleMatrix* p= createTriangleMatrix(x,leftBottom); for(int i=0;i<x*(x+1)/2;i++) { p->nums[i]=rand()%10; printf("%d ",p->nums[i]); } printf("\n"); printf("vertical corner at leftBottom:\n"); OuptPut(p); printf("vertical corner at rightBottom:\n"); p->triangleTye = rightBottom; OuptPut(p); printf("vertical corner at leftUp:\n"); p->triangleTye = leftUp; OuptPut(p); printf("vertical corner at rightUp:\n"); p->triangleTye = rightUp; OuptPut(p); printf("Done\n"); scanf("%d",&x);}
队列堆栈和数组-三角矩阵
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。