打印沙漏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 ((i*i) * 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;

}

 

posted @ 2019-02-21 18:50  gyyyy  阅读(63)  评论(0编辑  收藏  举报