// test2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<malloc.h>
void getR(int n);
int main(int argc, char* argv[])
{
printf("Hello World!\n");
getR(4);
return 0;
}
void getR(int n)
{
// 长度:1+2+...+n
int length = (1+n)*n/2;
// 动态申请数组
int *a = (int *)malloc(length * sizeof(int));
// 从第1行到第n行
for(int i=1;i<=n;i++)
{
// 当前行最后一个数的索引
int j=(1+i)*i/2-1;
// 当前行最后一个数的值:i
a[j--]=i;
// 从右往左的差值:n-1、n-2...
for(int k=1,d=n-1;k<i;j--,k++,d--)
{
a[j]=a[j+1]+d;
}
}
// 打印
for(int j=0,k=0;j<n;j++)
{
for(int m=0;m<=j;m++)
{
printf("%2d ", a[k++]);
}
printf("\n");
}
}