解题思路:
首先按题意分4类,假设d为德分,c为才分
1.前提首先d和c必须大于L
2.第一类:d>H && c>H 才德全尽
3.第二类:d>H && c<H 德胜才
4.第三类:d<H && c<H && d>c 才德兼亡,但尚有德胜才
5.第四类: 其他
6.排序:(1.分类,2.分类总分排序,3.总分相同德分排序,4.德分相同学号顺序)
中间有3个测试用例容易超时,go超时问题,逻辑都正常的话,一般出现在IO层,我把输入由scanf换成了bufio就过了
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
)
type Student struct{
number int //学号
dScore int //德分
cScore int //才分
status int8 //类型
total int //总数
}
type Students []Student
func (s Students) Len() int {
return len(s)
}
func (s Students) Less(i, j int) bool {
if s[i].status == s[j].status {
if s[i].total == s[j].total {
if s[i].dScore == s[j].dScore {
return s[i].number < s[j].number
} else {
return s[i].dScore > s[j].dScore
}
} else {
return s[i].total > s[j].total
}
} else {
return s[i].status > s[j].status
}
}
func (s Students) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func main() {
var n,l,h int
var inputReader *bufio.Reader
inputReader = bufio.NewReader(os.Stdin)
_, _ =fmt.Scanf("%d %d %d", &n, &l, &h)
stu := make(Students, n)
j:=0
for i:=0; i<n; i++ {
str,_ := inputReader.ReadString('\n')
strArray := strings.Fields(str)
num,_ := strconv.Atoi(strArray[0])
dScore, _ := strconv.Atoi(strArray[1])
cScore, _ := strconv.Atoi(strArray[2])
if dScore < l || cScore < l {
continue
}
stu[j].number = num
stu[j].dScore = dScore
stu[j].cScore = cScore
if dScore >= h && cScore >= h {
stu[j].status = 4
} else if dScore >= h && cScore < h {
stu[j].status = 3
} else if dScore < h && cScore < h && dScore >= cScore {
stu[j].status = 2
} else {
stu[j].status = 1
}
stu[j].total = stu[j].dScore + stu[j].cScore
j++
}
sort.Sort(stu)
fmt.Println(j)
for i:=0; i<j; i++ {
fmt.Printf("%d %d %d\n", stu[i].number, stu[i].dScore, stu[i].cScore)
}
}