2765: 练38.3 空心菱形

内存限制:64 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:11 解决:7

题目描述

输入一个整数 $n$,输出一个空心菱形,其中每个边由 $n$ 个'$*$'组成。

输入

一行一个整数 $n$($1< n < 20$)。

输出

输出一个空心菱形,每条边由 $n$ 个'$*$'组成。

样例输入 复制

3

样例输出 复制

  *
 * *
*   *
 * *
  *

提示

#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
    cin>>n;
    for(int i=1,sp=n-1;i<n;i++,sp--) {
    	for(int j=1;j<=sp;j++)
    		cout<<' '; 
		for(int j=1;j<=2*i-1;j++) {
			if (j==1||j==2*i-1) cout<<'*';
			else cout<<' ';
		}
    	cout<<endl;
	}
	for(int j=1;j<=2*n-1;j++) {
		if (j==1||j==2*n-1) cout<<'*';
		else cout<<' ';
	}
	cout<<endl;
	for(int i=n-1,sp=1;i>=1;i--,sp++) {
    	for(int j=1;j<=sp;j++)
    		cout<<' '; 
		for(int j=1;j<=2*i-1;j++) {
			if (j==1||j==2*i-1) cout<<'*';
			else cout<<' ';
		}
    	cout<<endl;
	}
	return 0;
}