floor(sqrt(L)) <= <= <= ceil(sqrt(L))* >= L* .haveaniceday line should output hae and via ecy .L = len("haveaniceday") = 12sqrt(L) = sqrt(12) = 3.46floor(sqrt(L)) = floor(3.46) = 3ceil(sqrt(L)) = ceil(3.46) = 4 3 <= <= <= 4 3 * 3 = 9 - does not satisfy the condition * >= L3 * 4 = 12 - satisfies the condition4 * 4 = 16 - satisfies the condition, but occupies a larger area than the previous version 1. = = floor(sqrt(L)) 2. * < L = ceil(sqrt(L)) 3. * < L = ceil(sqrt(L)) func detectGridSize(l int) (int, int) { s := math.Sqrt(float64(l)) f := int(math.Floor(s)) if f * f >= l { return f, f } c := int(math.Ceil(s)) if f * c >= l { return f, c } return c, c } = 3 and = 4 ), we display our original row in the form of a matrix: have anic eday 1. i 0 2. j 0 3. i*N+j , , S[i*N+j] G[i][j] func populateGrid(g [][]byte, s string) { for i := range g { for j := range g[i] { if k := i * len(g[i]) + j; k < len(s) { g[i][j] = s[k] } } } } 1. j 0 2. i 0 3. G[i][j] , G[i][j] 4. " " func printGridColumns(g [][]byte) { for j := range g[0] { for i := range g { if (g[i][j] != 0) { fmt.Print(string(g[i][j])) } } fmt.Print(" ") } } package main import ( "fmt" "math" ) func main() { var s string fmt.Scanln(&s) m, n := detectGridSize(len(s)) g := make([][]byte, m) for i := range g { g[i] = make([]byte, n) } populateGrid(g, s) printGridColumns(g) } func detectGridSize(l int) (int, int) { s := math.Sqrt(float64(l)) f := int(math.Floor(s)) if f * f >= l { return f, f } c := int(math.Ceil(s)) if f * c >= l { return f, c } return c, c } func populateGrid(g [][]byte, s string) { for i := range g { for j := range g[i] { if k := i * len(g[i]) + j; k < len(s) { g[i][j] = s[k] } } } } func printGridColumns(g [][]byte) { for j := range g[0] { for i := range g { if (g[i][j] != 0) { fmt.Print(string(g[i][j])) } } fmt.Print(" ") } } package main import ( "fmt" "math" ) func main() { var s string fmt.Scanln(&s) m, n := detectGridSize(len(s)) encodeString(m, n, s) } func detectGridSize(l int) (int, int) { s := math.Sqrt(float64(l)) f := int(math.Floor(s)) if f * f >= l { return f, f } c := int(math.Ceil(s)) if f * c >= l { return f, c } return c, c } func encodeString(m, n int, s string) { for j := 0; j < n; j++ { for i := 0; i < m; i++ { if k := i * n + j; k < len(s) { fmt.Print(string(s[k])) } } fmt.Print(" ") } } Source: https://habr.com/ru/post/277865/
All Articles