POJ字符菱形

字符菱形
查看 提交 统计 提问
总时间限制: 1000ms 内存限制: 65536kB
描述
给定一个字符,用它构造一个对角线长5个字符,倾斜放置的菱形。
输入
输入只有一行, 包含一个字符。
输出
该字符构成的菱形

#include <iostream>
using namespace std;
const int N = 5;

void delta(int n, char c)
{
	//打印上半身
	for (int i = 1; i <= n / 2; i++)//第i行
	{
		//打印空格,每行有n/2 - i+1个
		for (int j = 0; j < n/2 - i+1; j++)
			cout << " ";
		//打印三角,每行有2i-1个
		for (int j = 0; j < 2*i - 1; j++)
			cout << c;
		//换行
		cout << endl;
	}
	//打印中间一行
	for (int i = 1; i <= n; i++)
		cout << c;
	cout << endl;

	//打印下半身
	for (int i = 1; i <= n / 2; i++)//第i行
	{
		//打印空格,每行有i个
		for (int j = 0; j <  i; j++)
			cout << " ";
		//打印三角,每行有n-2i个
		for (int j = 0; j < n-2*i; j++)
			cout << c;
		//换行
		cout << endl;
	}
}

int main()
{
	char c;
	cin >> c;
	delta(N, c);
	return 0;
}
发布了16 篇原创文章 · 获赞 3 · 访问量 300

猜你喜欢

转载自blog.csdn.net/bajiaoyu517/article/details/104018737