问题描述
小蓝老师教的编程课有 N 名学生, 编号依次是 1…N 。第 i 号学生这学期 刷题的数量是 Ai 。
对于每一名学生, 请你计算他至少还要再刷多少道题, 才能使得全班刷题 比他多的学生数不超过刷题比他少的学生数。
输入格式
![](https://i-blog.csdnimg.cn/blog_migrate/d70f5350f0c9ea75b56cbae6365c3147.png)
输出格式
输出 N 个整数, 依次表示第 1…N 号学生分别至少还要再刷多少道题。
样例输入
5
12 10 15 20 6
样例输出
0 3 0 0 7
评测用例规模与约定
![](https://i-blog.csdnimg.cn/blog_migrate/21d54891bf68fa942798888fb5ea6520.png)
运行限制
最大运行时间:1s
最大运行内存: 512M
源码:
import java.util.Arrays;
import java.util.Scanner;
public class 最少刷题数 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int[] a=new int[n];
int[] b=new int[n];
for (int i = 0; i < n; i++) {
a[i]=scanner.nextInt();
b[i]=a[i];
}
Arrays.sort(a);
int mid=a[n/2];
int less=0,more=0;
for (int i = 0; i < a.length; i++) {
if (a[i]<mid) {
less++;
}
if (a[i]>mid) {
more++;
}
}
for (int i = 0; i < b.length; i++) {
if (b[i]>mid) {
System.out.print(0+" ");
}else if(b[i]==mid) {
if (less<more) {
System.out.print(1+" ");
}else {
System.out.print(0+" ");
}
}else {
if (less>more) {
System.out.print(mid-b[i]+" ");
}else {
System.out.print(mid-b[i]+1+" ");
}
}
}
}
}