打印空心正方形

题目描述

打印空心正方形

输入描述

输入一个整数n,1<=n<=100

输出描述

输出一个有字符'*'构成的空心正方形(只有边界上有'*')

样例输入

5

样例输出

*****
*   *
*   *
*   *
*****
 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int n;
 6     cin>>n;
 7     for(int i=0;i<n;i++)
 8     {
 9         if(i==0||i==n-1){
10             for(int j=0;j<n;j++)
11             {
12                 cout<<"*";
13             }
14             cout<<endl;    
15         }else{
16             cout<<"*";
17             for(int j=0;j<n-2;j++)
18             {
19                 cout<<" ";
20             }
21             cout<<"*";
22             cout<<endl;
23         }
24         
25     }
26 }

猜你喜欢

转载自www.cnblogs.com/palx/p/10512383.html