打印漏斗

https://pintia.cn/problem-sets/994805046380707840/problems/994805145370476544

*****
 ***沒空格
  *沒空格
 ***沒空格
*****

坑:輸出圖形後面沒有空格,只是前面有空格。

#include <iostream>
#include <algorithm>
#include <map>

using namespace std;


int main() {
	int n;
	int sum = 0;
	int cnt = 1;
	char sign;
	cin >> n >> sign;
	if (n < 1)
	{
		cout << 0;
		return 0;
	}
	while ((sum + (cnt + 2)) * 2 + 1 < n)
	{
		cnt += 2;
		sum += cnt;
	}

	// 正向輸出,包含單獨的一個
	for (int i = cnt; i >= 1; i-=2)
	{
		for (size_t j = 0; j < (cnt - i) / 2; j++)
		{
			cout << " ";
		}

		for (size_t j = 0; j < i; j++)
		{
			cout << sign;
		}

		cout << endl;
	}


	// 反向輸出,不包含中間那一個
	for (int i = 3; i <= cnt; i+=2)
	{
		for (size_t j = 0; j < (cnt - i) / 2; j++)
		{
			cout << " ";
		}

		for (size_t j = 0; j < i; j++)
		{
			cout << sign;
		}

		cout << endl;
	}
	
	cout << n - (sum * 2  + 1);
	
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章