PAT乙 1015 德才论 (GO实现)

解题思路:
首先按题意分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)
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值