C++输出平行四边形和菱形

#include<iostream>
int main()
{
	using namespace std;
	const int N = 4;
	//前四行
	for (int i = 1; i <= N; i++)
	{
		for (int j = 1; j <= 30; j++) //在图案左侧空30列
			cout << " ";
		for (int j = 1; j <= 8 - 2 * i;j++)
			cout << " ";
		for (int j = 1; j <= 2 * i - 1; j++)
			cout << "*";
		    cout << endl;

	}
	//后三行
	for (int i = 1; i < N; i++)
	{
		for (int j = 1; j <= 30; j++)//在图案左侧空30列
			cout << " ";
		for (int j = 1; j <= 7 - 2 * i; j++)
			cout << "*";
			cout << endl;

	}
}

结果如下:
在这里插入图片描述
如果要想输出如下图案:
在这里插入图片描述

#include<iostream>
int main()
{
	using namespace std;
	const int N = 4;
	//前四行
	for (int i = 1; i <= N; i++)
	{
		for (int j = 1; j <= 30; j++) //输出
			cout << " ";
		for (int j = 1; j <= 8 - 2 * i;j++)
			cout << " ";
		for (int j = 1; j <= 2 * i - 1; j++)
			cout << "*";
		    cout << endl;

	}
	//后三行
	for (int i = 1; i <= N; i++)
	{
		for (int j = 1; j <= 22; j++)
			cout << " ";
		for (int j = 1; j <= 9 - 2 * i; j++)
			cout << "*";
			cout << endl;

	}
}

输出菱形

#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	//外层循环控制行、换行
	//内层循环控制列、列的图形
	//中分的三角菱形
	//定义半菱形高度,即定义了菱形的大小
	int n;
	cout << "请输入半菱形高度" << endl;
	cin >> n;
	//上半部分居中正三角n行
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n - i - 1; j++)
		{
			cout << " ";
		}
		for (int j = 0; j <= 2 * i; j++)
		{
			//每行第一个和最后一个外打印星号,其余打印空格,做成空心效果,下半部分相同
			if (j == 0 || j == 2 * i)
				cout << "*";
			else
				cout << " ";
		}
		cout << endl;
	}

	//下半部分居中倒三角n-1行
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j <= i; j++)
		{
			cout << " ";
		}
		for (int j = 0; j <= 2 * (n - i - 2); j++)
		{
			if (j == 0 || j == 2 * (n - i - 2))
				cout << "*";
			else
				cout << " ";
		}
		cout << endl;
	}
	return 0;
}

在这里插入图片描述

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

量子孤岛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值