您的位置:首页 > 其它

蓝桥杯 打印十字图

2016-03-12 22:52 483 查看

问题描述

小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示:

[code]..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..


对方同时也需要在电脑dos窗口中以字符的形式输出该标志,并能任意控制层数。

输入格式

一个正整数 n (n<30) 表示要求打印图形的层数。

输出格式

对应包围层数的该标志。

样例输入1

[code]1


样例输出1

[code]..$$$$$..
..$...$..
$$$.$.$$$
$...$...$
$.$$$$$.$
$...$...$
$$$.$.$$$
..$...$..
..$$$$$..


样例输入2

[code]3


样例输出2

[code]..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..


提示

请仔细观察样例,尤其要注意句点的数量和输出位置。

分析

是时候祭出这张图了,再次原谅我拙劣的画图技能……



代码

[code]#include <iostream>
#include <vector>
using namespace std;

void printVector2(vector<vector<char> > res) {
    for (int i = 0; i < res.size(); i++) {
        for (int j = 0; j < res.size(); j++)
            cout << res[i][j];
        cout << endl;
    }
}

void changeAtRow(int start, int end, int row, vector<vector<char> > &res) {
    for (int col = start; col <= end; col++) 
        res[row][col] = '$';       
}

void changeAtCol(int start, int end, int col, vector<vector<char> > &res) {
    for (int row = start; row <= end; row++) 
        res[row][col] = '$';

}

void printFlag(int n) {
    int  length = 1 + 4 * n + 2 * 2;
    vector<vector<char> > res(length, vector<char>(length, '.'));    
    for (int i = n; i >= 0; i--) {
        int start = 2 * (n - i) + 2;
        int end = start + 1 + i * 4 - 1;
        int row = 2 * (n - i);
        int row2 = row + i * 4 + 4;
        changeAtRow(start, end, row, res);
        changeAtRow(start, end, row2, res);
        changeAtCol(row, row + 2, start, res);
        changeAtCol(row, row + 2, end, res);
        changeAtCol(row2 - 2, row2, start, res);
        changeAtCol(row2 - 2, row2, end, res);
    }
    for (int i = n; i >= 0; i--) {
        int start = 2 * (n - i) + 2;
        int end = start + 1 + i * 4 - 1;
        int col = 2 * (n - i);
        int col2 = col + i * 4 + 4;
        changeAtCol(start, end, col, res);
        changeAtCol(start, end, col + i * 4 + 4, res);
        changeAtRow(col, col + 2, start, res);
        changeAtRow(col, col + 2, end, res);
        changeAtRow(col2 - 2, col2, start, res);
        changeAtRow(col2 - 2, col2, end, res);
    }        
    printVector2(res);
}

int main(int argc, char *argv[]) {
    int n;
    cin >> n;
    printFlag(n);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: