方框
Description
用'*'打印出一个nxn的字符图形(1<=n<=100).
Input
多组输入。每行输入一个n,输入EOF结束文件.
Output
输出一个满足题意的图形.
Sample Input 1
1 2 5 6 10 11
Sample Output 1
* ** ** ***** * * * * * * * ***** ****** * * * ** * * ** * * * ****** ********** * * * ****** * * * * * * * ** * * * * ** * * * * * * * ****** * * * ********** *********** * * * ******* * * * * * * * *** * * * * * * * * * * *** * * * * * * * ******* * * * ***********
很有意思的输出图形题目,一开始总在想每一行每一行的输出,所以一直在找规律,就把自己找死了,今天再看这道题,突然发现,图形可以看作是一圈一圈围起来的,而且用二维数组来模图案会比每行输出方便得多,就很容易实现了,A掉
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define maxn 105
#define mod 1e9+7
typedef long long ll;
ll n;
char A[maxn][maxn];
void slove(ll x,ll len) //对正方形每个边填充
{
for(ll i = x; i <= len+x-1; i++ ) //上
A[x][i] = '*';
for(ll i = x; i <= len+x-1; i++) //左
A[i][x] = '*';
for(ll i = x; i <= len+x-1; i++) //下
A[x+len-1][i] = '*';
for(ll i = x; i <= len+x-1; i++) //右
A[i][x+len-1] = '*';
return ;
}
int main()
{
ios::sync_with_stdio(false);
while(cin >> n)
{
memset(A,' ',sizeof(A));
for(ll i = 1,len = n; i <= n; i += 2,len -= 4)
{
slove(i,len); //len为正方形边长 i为正方形边的行列
}
for(ll i = 1; i <= n; i++)
{
for(ll j = 1; j <= n; j++)
{
cout << A[i][j];
}
cout << endl;
}
}
return 0;
}