C语言上机试题:8
1. 题目:编一C程序,它能读入一个正整数n,然后输出如下图形:
(注:可执行文件名为e8_1.exe)
程序设计:
main()
{static char a[80][80];
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]='*';
}
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");
}