// STACK.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define N 6
int init(int *&b)
{
b=(int *)malloc(sizeof(N*(N+1)/2+1));
return 0;
}
int value(int *b,int i,int j)
{
if (i>=j)return b[i*(i+1)/2+j];
else return b[N*(N+1)/2];
}
void display(int *b)
{
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
printf("%d\t",value(b,i,j));
}
putchar('\n');
}
putchar('\n');
for( i=0;i<N;i++)
{
for( int j=0;j<N;j++)
{
printf("%d\t",value(b,j,i));
}
putchar('\n');
}
}
int main()
{int *b;
init(b);
puts("压缩存储:");
for(int i=0;i<N*(N+1)/2+1;i++)
{
b[i]=i;
printf("%d\t",b[i]);
}
putchar('\n');
puts("打印:");
display(b);
return 0;
}
DS:三角矩阵
最新推荐文章于 2024-07-25 17:43:13 发布