gqdwcc @ 2022-04-12 11:28:05
package main
import (
"fmt"
"sort"
)
type Result struct {
Chinese int
Math int
Eng int
Id int
Sum int
}
type Results []Result
func (r Results) Len() int{
return len(r)
}
func (r Results)Swap(i,j int) {
r[i],r[j]=r[j],r[i]
}
func (r Results)Less(i,j int) bool {
if r[i].Sum != r[j].Sum{
return r[i].Sum > r[j].Sum
}
if r[i].Chinese != r[j].Chinese{
return r[i].Chinese > r[j].Chinese
}
return r[i].Id < r[j].Id
}
func main() {
var n int
fmt.Scanf("%d",&n)
res := make(Results,n)
for i := 0; i < n; i++ {
var a,b, c int
fmt.Scanf("%d %d %d",&a,&b,&c)
res[i]=Result{
Chinese: a,
Eng: c,
Math: b,
Id: i+1,
Sum: a+b+c,
}
}
sort.Sort(res)
for i :=0;i<5;i++{
fmt.Printf("%d %d\n",res[i].Id,res[i].Sum)
}
}
by Terrible @ 2022-04-12 11:39:13
@gqdwcc Go的fmt.Scanf好像还需要把行尾的\n
匹配掉。
fmt.Scanf("%d %d %d",&a,&b,&c)
改为fmt.Scanf("\n%d %d %d",&a,&b,&c)
by gqdwcc @ 2022-04-12 13:11:28
@Terrible 加\n那样可以。
很奇怪,其他地方,同样的题,不用改可以过。。