using System; public class Program { public static void Main() { string line; while ((line = Console.ReadLine()) != null) { //录入第一行 string[] strings = line.Split(' '); int[] nra = new int[strings.Length]; for (int i = 0; i < nra.Length; i++) { nra[i] = int.Parse(strings[i]) ; } int[] grade = new int [nra[0]]; int[] time = new int [nra[0]]; int maxgrade = nra[1]; int targetavg = nra[2]; long usetime = 0; //录入分数和复习用时 for (int i = 0; i < nra[0]; i++) { line = Console.ReadLine(); strings = line.Split(' '); grade[i] = int.Parse(strings[0]); time[i] = int.Parse(strings[1]); } float avg = 0; float sum = 0; foreach(int i in grade){ sum += i; } avg = sum / nra[0]; //贪心算时间 while((int)avg < targetavg){ int mintime = -1; int index = 0; for(int i = 0; i < nra[0]; i++){ if(grade[i] == maxgrade) continue; if(mintime == -1 || time[i] < mintime){ index = i; mintime = time[i]; } } grade[index]++; sum++; avg = sum / nra[0]; usetime += mintime; } Console.WriteLine(usetime); } } }
思路:每次从分数未满的课中选择需要最少复习时间的课进行复习,复习之后总分++,用时+=复习用时,课程分数++
这里有个坑,用时需要用long型,否则有个案例不通过