C语言上机试题:2
1. 题目:编一C程序,它能读入一正整数n,(n<10),并输出如下图形:
(可执行文件名为e2_1.exe)
程序设计:
main()
{static int a[20][20];
int n,i,j,k;
printf("Please inter n=");
scanf("%d",&n);
printf("\n");
for (i=0;i<n;i++)
{k=1;
for (j=0;j<2*n-1;j++)
{if (j>n-i-2) a[i][j]=i+1;
if (k<=2*i+1+n-i-2) k++;
else break;
}
}
for (i=0;i<n;i++)
for (j=0;j<2*n-1;j++)
{if (a[i][j]==0) a[i][j]=' ';
else a[i][j]+=48;
}
for (i=0;i<n;i++)
{printf("\n");
for (j=0;j<2*n-1;j++)
printf("%c",a[i][j]);
}
for (i=n-2;i>=0;i--)
{printf("\n");
for (j=0;j<2*n-1;j++)
printf("%c",a[i][j]);
}
printf("\n");
}