pat1027:打印沙漏

https://www.patest.cn/contests/pat-b-practise/1027

#include "stdio.h"

int main()
{
	int n, j, k, i = 1;
	char a;
	scanf("%d %c", &n, &a);
	while(i*i*2 - 1 <= n) ++i;
	k = i*2 - 3;
	for(i = k; i >= 1; i-=2)
	{
		for(j = 0; j < (k-i)/2; ++j)
			printf(" ");
		for(j = 0; j < i; ++j)
			printf("%c", a);
		printf("\n");
	}
	for(i = 3; i <= k; i+=2)
	{
		for(j = 0; j < (k-i)/2; ++j)
			printf(" ");
		for(j = 0; j < i; ++j)
			printf("%c", a);
		printf("\n");
	}
	printf("%d\n", n-((k+1)*(k+1)/2-1));

	return 0;
}


你可能感兴趣的:(c,pat)