Java实现思路:创建一个志愿者内部类,包括编号、服务时长、难度、贡献度属性。使用ArrayList将多个志愿者对象联系在一起。使用Collection的sort方法,重写compare方法进行按规则比较。
Java代码:
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class Main{ static class volunteer{ public int id; public int t; public int k; public int conty; public volunteer(int id,int t, int k) { this.id = id; this.t = t; this.k = k; conty = t*k; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); List<volunteer> list = new ArrayList<>(); for (int i = 1; i <= n; i++) { list.add(new volunteer(i,sc.nextInt(),sc.nextInt())); } Collections.sort(list, new Comparator<volunteer>() { public int compare(volunteer o1, volunteer o2) { if (o1.conty != o2.conty) { return o2.conty - o1.conty; }else if(o1.t != o2.t) { return o2.t - o1.t; }else { return o1.id - o2.id; } } }); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i).id+" "); } } }