本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印
所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。
给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。
(1)实验代码
···
include <stdio.h>
int main()
{
int n;
char c;
scanf("%d %c",&n,&c);
int sum=1;//沙漏需要的字符个数
int i=1;//层数
while(sum<=n)
{
sum+=2(2(i+1)-1);//2n-1是一层的个数,乘以2代表两层一共需要的
if(sum<=n)
i++;
}
int j,k;
for(j=0;j<i;j++)//打印上半部分
{
for(k=0;k<j;k++)
printf(" ");
for(k=0;k<2(i-j)-1;k++)
{
printf("%c",c);
}//输出
printf("
");
}
for(j=2;j<=i;j++)
{
for(k=0;k<i-j;k++)
printf(" ");
for(k=0;k<2*j-1;k++)
{
printf("%c",c);
}
printf("
");
}
printf("%d",n-(sum-2*(2*(i+1)-1)));
}
(2)设计思路
(3)问题及解决办法
问题:不知道如何表示出打印的最大沙漏层数
解决办法:自己调试了一下没有结果,后面问了同学,讲解了一下理解了
还有错误截图没有保存
(4)结果截图