Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money
【PTA group Program Design Ladder Competition 】
L1-039 ancient layout (20 points) Go language | Golang
The ancients of China wrote characters in vertical layout from right to left. Please write a program to type a paragraph of text according to the ancient style.
Input format:
The input gives a positive integer N (<100) on the first line, which is the number of characters in each column. The second line gives a non-empty string of up to 1000 length, ending with a carriage return.
Output format:
Type the given string in archaic format, N characters per column (except for the last column, which may be less than N).
Input Example 1:
4
This is a test case
Copy the code
No blank line at the end
Example 1:
asa T
st ih
e tsi
ce s
Copy the code
No blank line at the end
Ideas:
Divide the length of the string by the number of characters, and then fill in a two-dimensional array. I’m still stuck typing… The idea should be no problem ~
The code is as follows:
package main
import "fmt"
func main(a) {
var num int
var str string
c:=0
num = 4
str ="This is a test case"
row:=len(str)/num
flag := false
var result [100] [100]rune
for i := row ; i >= 0 ; i-- {
for j := 0 ; j < num ; j++ {
if c == len(str) { // Note that without this, the subscript of STR may burst due to the following deficiency
flag=true / / because
break
}
result[j][i] = rune(str[c])
c++
}
if flag {
break}}for i := 0; i < row; i++ {
for j:=0; j<=num; j++{ fmt.Printf("%c",result[i][j])
}
ifi ! = row- 1 {
fmt.Println()
}
}
}
/*
4
This is a test case
*/
Copy the code