这是我的算法。运用了递归。太浪费空间。
#include<stdio.h>
#include<stdlib.h>
char **s;
int n;
FILE *fp;
void fun(int x)
{int i;
char *p;
if(x>1)
fun(x-1);
p=(char *)calloc((2*x-1),sizeof(char));
s[x-1]=p;
for(i=0;i<2*x-1;i++)
p[i]=x+48;
for(i=0;i<n-x;i++)
{
printf(" ");
fprintf(fp," ");
}
for(i=0;i<2*x-1;i++)
{if(p[i]>'9')
p[i]='A'-'9'+p[i]-1;
printf("%c",p[i]);
fprintf(fp,"%c",p[i]);
} printf("\n");
fprintf(fp,"\n");
}
void main()
{
fp=fopen("xy.txt","w");
int x=1;
int i,j;
scanf("%d",&n);
s=(char **)calloc(n,sizeof(char *));
fun(n);
for(i=1;i<=n-1;i++)
{for(j=0;j<i;j++)
{
printf(" ");
fprintf(fp," ");
}
printf("%s",s[n-1-i]);
fprintf(fp,"%s",s[n-1-i]);
printf("\n");
fprintf(fp,"\n");
}
}