#include <math.h>
int main()
{int n,i,j
scanf("%d",&n)
printf("%*d\n",n,n)
for(i=2-ni<n-1i++)
{if(i)printf("%*c",abs(i),' ')
for(j=nj>1+abs(i)j--)
printf("%d",j)
for(j<=nj++)
printf("%d",j)
printf("\n")
}
if(n>1)printf("%*d\n",n,n)
return 0
}
turn('',k)应该打多了空格吧!而且超过10的时候就不行了!
所以改了一下:
#include <stdio.h>
int num
void turn(char ch,int n)
{
if(n>0)
{
if(ch==' ')
{
printf("%2c",ch)
turn(ch,n-1)
}
else
{
if(num<10)
printf("%2c",ch)
else
printf("%2d",(int)(ch-'0'))
turn((char)(ch-1),n-1)
}
}
}
void turn1(char ch,int k,int n)
{
if(k<n)
{
if(num<10)
printf("%2c",ch)
else
printf("%2d",(int)(ch-'0'))
turn1((char)(ch+1),k+1,n)
}
}
int main()
{
int i,k
scanf("%d",&num)
for(i=1i<=2*num-1i++)
{
k=(i<=num)?num-i:i-num
turn(' ',k)
turn((char)(num+'0'),num-k)
turn1((char)(k+2+'0'),k+1,num)
printf("\n")
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)