华为机考练习(golang)
题目描述 在斗地主只扑克牌游戏中,扑克牌由小到大的顺序为:3.4,5.6,7.8,9,10.J,Q.K.A.2,玩家可以出的扑克牌阵型有:单张、对子、顺子、飞机、炸弹等。 其中顺子的出牌规则为:由至少5张由小到大连续递增的 扑克牌只 组成,且不能包含2。
例如:(3.4,5,6,7}、(3.4,5,6,7,8,9,10,J,Q,K,A}都是有效的顺子;而{,Q,K,A,2}、(2,3,4,5,6}、(3,4,5,6}、(3,4,5.6,8)等都不是顺子给定一个包含 13 张牌的数组,如果有满足出牌规则的顺子,请输出顺子。 如果存在多个顺子,请每行输出一个顺子,且需要按顺子的第一张牌的大小(必须从小到大)依次输出。 如果没有满足出牌规则的顺子,请输出NO。输入描述: 13张任意顺序的扑克牌,每张扑克牌数字用空格隔开,每张扑克牌的数字都是合法的,并且不包括大小王:2 9 J 2 3 4 K A 7 9 A 5 6不需要考虑输入为异常字符的情况输出描述: 组成的顺子,每张扑克牌数字用空格隔开:3 4 5 6 7
示例1
输入
2 9 J 2 3 4 K A 7 9 A 5 6
输出
3 4 5 6 7 <
package main
import (
"errors"
"fmt"
"log"
"sort"
"strings"
)
func getIndex(list []string, value string) (int, error) {
for i, s := range list {
if s == value {
return i, nil
}
}
return 0, errors.New("元素不存在")
}
func removeSliceElements(list []int, removeIndex []int) (subList []int) {
for i, v := range list {
isNeedRemove := false
for _, indexValue := range removeIndex {
if i == indexValue {
isNeedRemove = true
}
}
if !isNeedRemove {
subList = append(subList, v)
}
}
return
}
// 获取数组里的一个顺子
func getContinuePoker(list []int) (subList []int, continuePoker []int) {
if len(list) < 5 {
return list, make([]int, 0)
}
continuePokerIndexList := make([]int, 0)
continuePokerTemp := make([]int, 0)
for i, v := range list {
if i == 0 {
continuePokerTemp = append(continuePokerTemp, v)
continuePokerIndexList = append(continuePokerIndexList, i)
continue
}
subValue := v - list[i-1]
if subValue == 0 {
//出现重复数字
continue
} else if subValue == 1 {
//连续
continuePokerTemp = append(continuePokerTemp, v)
continuePokerIndexList = append(continuePokerIndexList, i)
} else {
//不连续
break
}
}
continuePoker = continuePokerTemp
//删除输入切list中对应的顺子的元素
subList = removeSliceElements(list, continuePokerIndexList)
return subList, continuePoker
}
// 获取数组里的全部顺子
func getContinuePokers(list []int, continuePokers [][]int) [][]int {
if subList, continuePoker := getContinuePoker(list); len(subList) >= 5 {
if len(continuePoker) >= 5 {
continuePokers = append(continuePokers, continuePoker)
}
return getContinuePokers(subList, continuePokers)
} else {
return continuePokers
}
}
func printPokers(continueList [][]int, templateIndex []string) {
if len(continueList) == 0 {
log.Println("NO")
} else {
for _, s := range continueList {
tempString := ""
for _, i := range s {
if tempString == "" {
tempString = fmt.Sprintf("%s", templateIndex[i])
} else {
tempString = fmt.Sprintf("%s %s", tempString, templateIndex[i])
}
}
log.Println(tempString)
}
}
}
func main() {
templateIndex := []string{"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"}
inputStr := "2 3 4 5 6 7 10 J Q K A 2 3 4 5 6 7 10 J Q K A 10 J Q K A 2 3 4 5 6 7 7 10 J Q K A 10 J Q 8 8 8 9 9"
inputStrList := strings.Split(inputStr, " ")
inputIndexList := make([]int, 0)
for _, item := range inputStrList {
if item == "" {
continue
}
//排除掉“2”
if item == "2" {
log.Println("去除掉元素‘2’ ")
continue
}
if itemIndex, err := getIndex(templateIndex, item); err != nil {
log.Println(fmt.Sprintf("输入的:%d 不符合规则", itemIndex))
continue
} else {
inputIndexList = append(inputIndexList, itemIndex)
}
}
if len(inputIndexList) == 0 {
return
} else {
//按照纸牌大小重新牌型
sort.Ints(inputIndexList)
//查找顺子牌
continueList := make([][]int, 0)
continueList = getContinuePokers(inputIndexList, continueList)
printPokers(continueList, templateIndex)
}
}