This is the 30th day of my participation in the August Challenge
【PTA group Program Design Ladder Competition 】
L1-015 draw squares (15 points) with Obama Go language | Golang
US President Barack Obama has become the first US President to write computer code, not only calling for everyone to learn to code, but even leading by example. In late 2014, to mark the official launch of Computer Science Education Week, Obama wrote simple computer code: Draw a square on a screen. Now you can draw with him!
Input format:
Enter a line that gives the square side length N (3≤N≤21) and some character C that makes up the square side, separated by a space.
Output format:
Prints the square drawn by the given character C. But notice that the spacing between rows is larger than the spacing between columns, so to make the result look more square, we actually output the number of rows as 50% of the number of columns (rounded).
Example Input:
10 a
Copy the code
No blank line at the end
Example output:
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
Copy the code
No blank line at the end
Ideas:
The basic data structure type problem, which is defined as float64, should be defined as float64, because Go language is strongly typed language, so must always remember! Whatever the number was originally defined to be, the result of the calculation or the calculation is of that type.
For example, if a float64 is defined as float64, the value of a float64 is 15, then it is divided by 2 and becomes 7.
The code is as follows:
package main
import "fmt"
func main(a) {
var line float64
// If line is 15, then line/2 is 7, not 7.5. If line/2 is 7, then it cannot be rounded by +0.5.
var s string
_,_=fmt.Scan(&line,&s)
row := int(line/2+0.5) //+0.5 rounded
for i:=0; i<row; i++{for j:=0; j<int(line); j++{ fmt.Printf("%s",s)
}
if i == row- 1 {
break
}
fmt.Println()
}
}
Copy the code
【PTA group Program Design Ladder Competition 】
L1-016 check id (15 points) Go language | Golang
A valid ID card number consists of a 17-digit area, date and sequence number plus a one-digit verification code. The calculation rules of the verification code are as follows:
First, the first 17 digits are weighted and summed. The weight assignment is: {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; Then modulo the calculated sum with respect to 11 to get the value Z; Finally, the Z value and the check code M are corresponding according to the following relation:
Z: 0 1 2 3 4 5 6 7 8 9 10 M: 1 0 X 9 8 July 6 5 4 3 2Copy the code
Now, given some ID numbers, verify the validity of the verification code and output the number in question.
Input format:
Input The first line shows that the positive integer N (≤100) is the number of id numbers to be entered. Then N lines, each line gives an 18-digit ID number.
Output format:
Output one problematic ID number per line in the input order. It does not check whether the first 17 bits are reasonable, but only whether the first 17 bits are all digits and the last 1 bit is calculated correctly. If All numbers are normal, All passed is displayed.
Input Example 1:
4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X
Copy the code
No blank line at the end
Example 1:
12010X198901011234
110108196711301866
37070419881216001X
Copy the code
No blank line at the end
Input Example 2:
2
320124198808240056
110108196711301862
Copy the code
No blank line at the end
Example 2:
All passed
Copy the code
No blank line at the end
Ideas:
A little bit complicated, but in fact, the implementation of the idea is very clear, I have written a lot of notes ~ according to the notes should be no problem!
The code is as follows:
package main
import (
"fmt"
)
func main(a) {
var num int
_,_=fmt.Scan(&num)
var ansList []string
weight := []int{7.9.10.5.8.4.2.1.6.3.7.9.10.5.8.4.2} // Use arrays to store weights
std := map[int]string{
0:"1".1:"0".2:"X".3:"9".4:"8".5:"Seven".6:"6".7:"5".8:"4".9:"3".10:"2",}// Use map to store key and value pairs
var strList []string
for i:=0; i<num; i++ {
var str string
_,_=fmt.Scan(&str)
strList = append(strList, str)
}
for _,str := range strList {
var flag bool
sum:=0
for index, s := range str {
if index == len(str)- 1 { // The last bit is the validation bit, which does not participate in the calculation
break
}
if '0' > s || s > '9' { // If it is not a numeric string, it is simply extrapolated and placed in the array
ansList = append(ansList, str)
flag = true // flag the number so that it does not participate in the following calculation
break
}
num := int(s - '0') // Each character is evaluated here, because the string reads the character ackII, so we want the -'0' character 0
// For example, '1' is equal to 49, and '0' is equal to 50, so we want '1' -'0' to be the integer 1.
sum += num * weight[index] // The number is multiplied by the corresponding weight
}
if flag { // If the number is due to a character that is not a number, skip the character directly.
continue
}
stdIndex := sum % 11 // Mod 11 of the weighted sum
stdStr := string(str[len(str)- 1]) // The last person to get this ID card, continue to verify
ifstd[stdIndex] ! = stdStr {// If the validation is unsuccessful, it is incorrect and added to the list
ansList = append(ansList, str)
}
}
if len(ansList) == 0 { // All passed is printed if the list has no numbers
fmt.Printf("All passed")}else {
for i, str := range ansList {
if i == 0 { // This is to control the newline problem
fmt.Printf(str)
} else {
fmt.Printf("\n"+str) // This ensures that the last line is not newline.}}}}Copy the code