标签:style blog color io os for sp div on
#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); }
标签:style blog color io os for sp div on
原文地址:http://www.cnblogs.com/kbyd/p/3995500.html