上机编程认证02
作者:互联网
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019-2021. All rights reserved.
* Description: 上机编程认证
* Note: 缺省代码仅供参考,可自行决定使用、修改或删除
* 只能import Go标准库
*/
package main
import (
"bufio"
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
)
// 待实现函数,在此函数中填入答题代码
func getTop3Num(input string) []int {
//按规则抽取所有的数字
//规则1.紧靠数字的-号表示负数
//规则2.前面的0需要去掉
//规则3.从大到小排序取前三
nums := []int{}
boxString := ""
boxTagForNegative := -1
boxTagForIntval := false
//boxTagForZero := 0
for key,value := range input {
if string(value) == "-" {
boxTagForNegative = key
}
if value >=48 && value <= 57 {
boxTagForIntval = true
}
if key - boxTagForNegative == 1 {
boxString = "-"
boxTagForNegative = -1
}
if boxTagForIntval {
boxString += string(value)
}
if !boxTagForIntval && boxTagForNegative == -1 || key == len(input) - 1{
boxInt,_ := strconv.Atoi(boxString)
nums = append(nums,boxInt)
boxString = ""
}
boxTagForIntval = false
}
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
return nums[0:2]
}
func main() {
inputReader := bufio.NewReader(os.Stdin)
lineBuf, err := inputReader.ReadString('\n')
if err != nil && err != io.EOF {
fmt.Println(err.Error())
return
}
input := strings.TrimRight(lineBuf, "\r\n")
result := getTop3Num(input)
fmt.Print("[")
for i := 0; i < len(result); i++ {
if i == 0 {
fmt.Print(strconv.Itoa(result[i]))
} else {
fmt.Print(" " + strconv.Itoa(result[i]))
}
}
fmt.Print("]")
}
a56b-6-8()_56!@$-a
提示:输入字符串四个数字 56,-6,-8,56 取最大
[56 56 -6]
--028.300003
提示:抽取 -28,300003
[300003 -28]
标签:02,56,boxString,上机,fmt,编程,value,input,boxTagForNegative 来源: https://www.cnblogs.com/gongxianjin/p/16123206.html