您的位置:首页 > 其它

普及练习场 贪心 排队接水

2017-12-15 17:06 169 查看
题目链接

题意理解

这个就是小学奥数题。花时间少的先干事就好了。注意一下接水的时间没有算在里面,因为要求的是等待时间。

代码

import java.util.Arrays;
import java.util.Scanner;

class Person implements Comparable{
int t;
int index;
@Override
public int compareTo(Object o) {
Person person = (Person) o;
if(person.t != this.t) {
return this.t - person.t;
} else {
return this.index - person.index;
}

}
}

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
Person[] persons = new Person
;
for(int i = 0; i < N; i++) {
persons[i] = new Person();
persons[i].t = scanner.nextInt();
persons[i].index = i + 1;
}
scanner.close();
Arrays.sort(persons);
double sum = 0;
for(int i = 0; i < N; i++) {
System.out.print(persons[i].index + " ");
sum += persons[i].t * (N - i - 1);
}
System.out.println();
System.out.printf("%.2f", sum / N);
}
}


欢迎加入“不会算法一群菜鸟”,群号是⑥⑥①⑨②2025,这是我设置的一道很低的门槛用来阻止广告的。入群的验证暗号是:我爱编译原理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: