自己试了一下,还行
程序代码:
#include <stdio.h>
#include <math.h>
#include <string.h>
#define N 20
int a[N][N];
int main()
{
int n, line; //输入数,与行数
int i, j, k;
int temp1 = 1;
int temp2;
memset(a, 0, N * N * 4);
printf("Please input n: "); scanf("%d", &n);
for (line = 1;line * (line + 1) / 2 < n;line++);
temp2 = line; //得到所需的行数
//思路:每次初始化一个三角形
for (i = 0;i < temp2;i++)
{
for (j = i;j <temp2 && temp1 <= n;a[i][j++] = temp1++); //横直角边
for (j -= 2, k = i + 1;j >= i && temp1 <= n;a[k++][j--] = temp1++);//斜边
for (k -= 2;k > i && temp1 <= n;a[k--][i] = temp1++); //竖直角边
temp2 -= 2;
}
printf("\nResult is:\n");
for (i = 0;i < line;i++)
{
for (j = 0;j < line - i;j++)
printf("%4d", a[i][j]);
printf("\n");
}
return 0;
}
[
本帖最后由 azzbcc 于 2012-12-8 22:50 编辑 ]