***********
*         *
* ******* *
* *     * *
* * *** * *
* * * * * *
* * *** * *
* *     * *
* ******* *
*         *
***********
观察这个图形,它是由一系列正方形的星号方框嵌套而成。
在上边的例子中,最外方框的边长为11。
本题的任务就是从标准输入获得一个整数n(1<n<100)
程序则生成嵌套着的回字型星号方框。其最外层方框的边长为n
例如:
输入:
5
程序输出:
*****
*   *
* * *
*   *
*****
输入:6
程序输出:
******
*    *
* ** *
* ** *
*    *

******

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    char graph[101][101];
    int n;
    cin>>n;
    for(int up=0,down=n-1; up<=down; up=up+2,down=down-2)
    {
        for(int i=up; i<=down; i++)
        {
            graph[up][i]='*';//上
            graph[down][i]='*';//下
            graph[i][up]='*';//左
            graph[i][down]='*';//右
        }
    }
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
            cout<<graph[i][j];
        cout<<endl;
    }
    return 0;
}


posted on 2015-04-07 20:23  星斗万千  阅读(147)  评论(0编辑  收藏  举报