练习---打印实心菱形

题目:

输入一个数字,打印出对应的实心菱形
例如输入3,则输出如下:
在这里插入图片描述
代码:

#include<iostream>
using namespace std;
int main(){
	int n = 0;
	cin>>n;
	//上半部分
	for(int i = 1;i <= n;i++){
		for(int j = 0;j <n-i;j++ ){
			cout<<" ";
		}
		for(int j = 0;j < 2*i-1;j++){
			cout<<"*";
		}
		for(int j = 0;j <n-i;j++ ){
			cout<<" ";
		}
		cout<<endl;
	} 
	//下半部分
	for(int i = n-1;i >= 1;i--){
		for(int j = 0;j <n-i;j++ ){
			cout<<" ";
		}
		for(int j = 0;j < 2*i-1;j++){
			cout<<"*";
		}
		for(int j = 0;j <n-i;j++ ){
			cout<<" ";
		}
		cout<<endl;
	} 
	system("pause");
	return 0;	
}
发布了102 篇原创文章 · 获赞 21 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/guanripeng/article/details/104598784