打印沙漏1

1 #include<stdio.h>

int main()
{
int sum;
char c;
int n;
scanf("%d %c", &sum, &c);
for (int i = 0; i <= 100; i++)
{
if ((ii) * 2 - 1 == sum)
{
n = i;
break;
}
else if ((i
i) * 2 - 1 > sum)
{
n = i - 1;
break;
}
}
int t = n;
int counter = 0;
while (t)
{
for (int i = 1; i <= counter; i++)
{
printf(" ");
}
for (int i = 1; i <= (2 * t - 1); i++)
{
printf("%c", c);
}

printf("\n");
counter++;
t--;
}
t = 2;
counter = n - 2;
while (t<=n)
{
for (int i = 1; i <= counter; i++)
{
printf(" ");
}
for (int i = 1; i <= (2 * t - 1); i++)
{
printf("%c", c);
}

printf("\n");
counter--;
t++;
}
if (sum == 0)
printf("0\n");
else
printf("%d\n", sum - (2 * n*n - 1));
system("pause");
return 0;
}

2 运用while语句打印出上半部分的行数,再用两个for语句确定上下两部分的行数,for语句中再镶嵌for语句确定“*”与空格的个数

3 while与for的顺序颠倒部分,然后请教教了同学

posted @ 2019-02-19 21:36  徐卓韩  阅读(129)  评论(0编辑  收藏  举报