Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Z-shape Transformation and arrangement in C language

Shulou Source: shulou.com Published: 2022-05-31 19:53:23 09月21日 Update

This article introduces the relevant knowledge of "how to realize Z-shaped transformation and arrangement in C language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Topic link: Z glyph transformation

Method one

-- finding rules to simulate the array

The topic requires the construction of a Z-type matrix from left to right.

Through the analysis, we can see the characteristics of this Z matrix.

The Z-type matrix is composed of orange and green parts in the picture, and the Z-type matrix is composed of one by one with the same period.

There is a case that requires special discussion: when the matrix has only one line, the original character is returned directly.

The rest of the situation is satisfactory.

The composition of its period satisfies the following law:

Fill in the matrix row number of r characters down in the first column, and then fill in one character in each of the (rmae2) columns on the upper right. The period t=r+r-2=2*r-2 of the Z-type matrix, each cycle occupies the Rmur1 column of the matrix, with a total character length of len/t cycles (the last period is regarded as a full cycle).

So create a two-dimensional matrix with r rows and c columns (here you need an extra period when calculating the columns, because the calculation of division will strip out the remainder). At the beginning, fill in the characters from the position of (0Cool 0), and determine whether to move up or down by judging the size of I% t and rmae1.

There are two situations:

I%t=r-1: indicates that the first column has been filled out, so you need to fill in the characters to the upper right, so you need to move one bit to the right and one bit up.

When a cycle is finished, it goes back to the first column of the new cycle.

Iterate through the matrix again, adding the location where the characters are stored into a new string.

Class Solution {public: string convert (string s, int numRows) {if (numRows==1 | | numRows > = s.size ()) / / Special case to exclude the number of rows of return s; int / matrices / number of rows of matrices / number of characters contained in cycles int len=s.size (); / / length of string int c = (len+t) / t* (rmur1) / / the number of two-dimensional matrix columns vector v1 (rjol string (cMago 0)); for (int iMago, Xerox 0meme yobo Tinci)

Tags: Matrix character period movement case complexity string array method space glyph transformation position part length two-dimensional language special line Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information Docker Xiaomi Microsoft