自测题I——打印沙漏(20)

#include 
using namespace std;
int main()
{
	int n, i = 1, q = 1;
	char c;
	cin >> n >> c;
	if (n <= 0){ cout << 0; return 0; }
	while (2 * q*q - 1<= n  && ++q);
	i = 2 * q - 3;
	for (int j = 1; j <= i; j++)
	{
		if (j > i / 2)
		{
			for (int k = 0; k < i-j; k++)
				cout << " ";
			for (int l = 0; l < 2*j-i; l++)
				cout << c;
		}
		else
		{
			for (int k = 0; k < j-1; k++)
				cout << " ";
			for (int l = 0; l < i - 2 * (j-1); l++)
				cout << c;
		}
		cout << endl;
	}
	cout << n - 2 * (q-1)*(q-1) + 1;
	return 0;
}

你可能感兴趣的:(PAT)