解題思路:
首先按題意分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)
}
}