JakeLin- [Blue Bridge Cup] [The 4th Zhenti in 2013] Print Cross Diagram-Problem Solution

Problem description 
Xiao Ming designed a cross-shaped logo for an institution (not the Red Cross Society), as shown below:

..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..

The other party also needs to output the logo in the form of characters in the computer dos window, and can control the number of layers arbitrarily. 

Input

A positive integer n (n <30) indicates the number of layers required to print graphics.  

Output

This flag corresponds to the number of surrounding layers.

Sample input

3  

Sample output

..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$.. 

Original title link: [Blue Bridge Cup] [The 4th Zhenti 2013] Print the cross

 

Simple graphic question, step-by-step analysis of the simulation process:

Step 1: Initialization (all '.')

int r = 5+n*4; //总共r行r列,5是中间的十字,加上左边2n,右边2n
    for(int i=1;i<=r;i++){
        for(int j=1;j<=r;j++){
        t[i][j]='.';
    }
}

result:


Step 2: Draw the cross in the middle (1/4 in the upper left corner)

for(int i=r/2+1-2;i<=r/2+1;i++){ //中间的十字 ,最中间的坐标为(r/2+1,r/2+1)
    t[r/2+1][i]='$';
    t[i][r/2+1]='$';
}

result:


Step 3: [Key] draw n layers around (1/4 in the upper left corner)

void quater(int r,int c,int w){
    t[r][c]='$';
    for(int i=1;i<=w;i++){ //向上走 w 
        t[--r][c]='$';
    }
    for(int i=1;i<=2;i++){  //向右走 2
        t[r][++c]='$';
    }
    for(int i=1;i<=2;i++){  //向上走 2
        t[--r][c]='$';
    }
    for(int i=1;i<=w;i++){  //向右走w 
        t[r][++c]='$';
    }    
} 
{    ...
    int row = r/2+1;
    int col = r/2+1-4;
    int walk=2;
    while(n--){  //左上角四分之一,每个n为一层 ,起点为(row,col) 
        quater(row,col,walk); 
        col-=2;
        walk+=2;
    }
}

result:


Step 4: Symmetry

for(int j=r;j>r/2+1;j--) {
   for(int i=1;i<=r/2+1;i++){
       t[i][j] = t[i][r-j+1];
   }
}

result:


Step 5: Symmetry up and down

for(int i=r;i>r/2+1;i--){
    for(int j=1;j<=r;j++){
        t[i][j] = t[r-i+1][j];
    }
}

result:

 So far, complete code is attached:

#include<cstdio>
#include<iostream> 
using namespace std;
char t[150][150];
void quater(int r,int c,int w){
    t[r][c]='$';
    for(int i=1;i<=w;i++){ //向上走 w 
        t[--r][c]='$';
    }
    for(int i=1;i<=2;i++){  //向右走 2
        t[r][++c]='$';
    }
    for(int i=1;i<=2;i++){  //向上走 2
        t[--r][c]='$';
    }
    for(int i=1;i<=w;i++){  //向右走w 
        t[r][++c]='$';
    }    
} 
int main(){
    int n;
    cin>>n;
    int r = 5+n*4;
    for(int i=1;i<=r;i++){
        for(int j=1;j<=r;j++){
            t[i][j]='.';
        }
    }
    for(int i=r/2+1-2;i<=r/2+1;i++){ //中间的十字 
        t[r/2+1][i]='$';
        t[i][r/2+1]='$';
    }
    int row = r/2+1;
    int col = r/2+1-4;
    int walk=2;
    while(n--){  //左上角四分之一,每个n为一层 ,起点为(row,col) 
        quater(row,col,walk); 
        col-=2;
        walk+=2;
    }
    //左右对称
    for(int j=r;j>r/2+1;j--) {
        for(int i=1;i<=r/2+1;i++){
            t[i][j] = t[i][r-j+1];
        }
    }
    //上下对称
    for(int i=r;i>r/2+1;i--){
        for(int j=1;j<=r;j++){
            t[i][j] = t[r-i+1][j];
        }
    } 
    for(int i=1;i<=r;i++){
        for(int j=1;j<=r;j++){
            cout<<t[i][j];
        }
        cout<<endl;
    }
    return 0;
}

 

Published 20 original articles · won 15 · views 217

Guess you like

Origin blog.csdn.net/qq_37414463/article/details/105375531