Queue stack and array-Triangle Matrix

Source: Internet
Author: User
#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);}

 

Queue stack and array-Triangle 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.