今天费了九牛二虎之力,总算把沙漏程序写出来了,主要运用的是方程组思想,最后w=0卡了我好久,果然c语言要十分注重书写的规范。
#include<stdio.h>
#include<math.h>
int main(void)
{
int x,y,z,t,re,n= 0;
char m;
int w = 0;
int q = 0;
scanf_s("%d %c", &x,&m,20);
y = (x - 1) / 2;
z = sqrt(y);
//printf("%d", z);
re = (pow(z, 2)) * 2 - 1;
n = x - re;
t = z*2-1;
for (t; t >= 0; t--)
{
if (t % 2 == 1)
{
for (q; q < (z*2-1-t)/2; q++)
{
printf(" ");
}
q = 0;
while (w < t)
{
w = w + 1;
printf("%c", m);
}
printf("\n");
}
w = 0;
}
for (int h = t + 3; h <= z * 2 - 1; h++)
{
if (h % 2 == 1)
{
for (q; q < (z * 2 - 1 - h) / 2; q++)
{
printf(" ");
}
q = 0;
while (w < h)
{
w = w + 1;
printf("%c", m);
}
printf("\n");
//printf("%d", w);
}
w = 0;
}
printf("%d", n);
}