解题思路:
身份证18位,前17位根据给出的权重,求加权值,以加权值%11的模为下标,取对应的校验码。比较身份证的第18位,如果不同则放入输出数组。当然如果前17位中存在非数字也放入输出数组。最后只需要判断输出数组是否为空,空则all passed,不为空依次输出即可
package main
import (
"fmt"
)
func main() {
var N int
var inputStr []string
var outputStr []string
var weight = []int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
var flag = []byte{'1','0','X', '9', '8', '7', '6', '5', '4', '3', '2'}
_, _ = fmt.Scanf("%d", &N)
inputStr = make([]string, N)
outputStr = make([]string, N)
for i:=0; i<N; i++ {
_, _ = fmt.Scanf("%s", &inputStr[i])
}
k := 0
for i:=0; i<N; i++ {
temp := inputStr[i]
sum := 0
j := 0
for ; j<17; j++ {
if temp[j] == 'X' {
outputStr[k] = temp
k++
break
}
sum += int(temp[j] - '0') * weight[j]
}
if j >= 17 {
z := sum % 11
if flag[z] != temp[17] {
outputStr[k] = temp
k++
}
}
}
if k == 0 {
fmt.Println("All passed")
} else {
for i:=0; i<k; i++ {
fmt.Println(outputStr[i])
}
}
}