Blue Bridge - graphic letters

Problem Description

Use of the letters can form some beautiful graphics, an example is given below:

ABCDEFG

BABCDEF

CBABCDE

DCBABCD

EDCBABC

This is a pattern row 5 7, find the pattern of this rule, and outputs a pattern of n rows and m columns.

Input format
input line, comprising two integers n and m, respectively represent the number of columns you want output line pattern.
Output format
output n lines, each m characters, for your pattern.
Input Sample
57
Sample Output
ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC
data size and Conventions
1 <= n, m <= 26.

Analysis : According to the conventional understanding, we should be thinking to construct a two-cycle, controlling the number of outer rows, each row of the inner layer inside the output control element, i.e., the number of columns. Like this pseudo-code:

cin>>n>>m;
for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
        //控制每行元素输出语句
    }
}

Analysis: Now we look at regular arrangement of the internal elements, five sample input line 7, the output is:

ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC

What can be seen? If we look at the arrangement according to their ASCLL code values, generally such a result can be obtained: the first line has been increased from the first element ASCLL A start code value, the number of columns until the completion of output, the second row down to first B a, rises again, and so on, the latter then decreased at a faster pace, then rises, moves to the right section like a line of upwardly opening V-shaped in m x-axis (column), then a loop inside what kind of situation the output of this sequence? The answer is the difference between the two numbers of absolute value , look at the code:

Attach a code value for each row ASCLL FIG curve shifts:

#include <iostream>
using namespace std;

int main()
{
	int n, m, j, k;
	char x = 'A';
	cin >> n >> m;
	if (n >= 1 && m <= 26)
		for (j = 1; j <= n; j++)
		{
			for (k = 1; k <= m; k++)
				cout << (char)(x + (j >= k ? j - k : k - j));
			cout << endl;
		}
	return 0;
}

 

Here can also call an absolute value function, x is the difference between j and k plus: first cycle, j = 1, k = 1, so the initial value x A plus 0 or A, second cycle, to add a k-th cycle k, then k> j, so to x + 1, an output B, ..., a second round of the cycle, j = 2, k = 1, so the first output value is B, then k ++; j and k and the difference is 0, the output a, next, then k increase, the difference is gradually increased until the difference between the current round of the cycle is completed after ...... j and k each cycle of a value of the initial value will gradually increase, so every time the first value of each line on the line than the freshman, so far in this way can fully understand it.

Output:

 

Published 51 original articles · won praise 5 · Views 2019

Guess you like

Origin blog.csdn.net/MARS_098/article/details/103209079