电梯
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Problem Description
现在有n个人在一楼准备坐电梯,这个电梯最多能装m个人,我们想知道n个人都到达目标楼层,电梯回到一
楼最少走了多少层
。
Input
本题有多组测试数据,每组数据有两行
第一行,两个数字n和m.(1<=n,m<=1000)
第二行包含n个数字,每个数字表示他要去的楼层(1<=楼层<=10000).
Output
每组数据输出一行。
Sample Input
6 6 6 6 6 6 6 6
Sample Output
10
Hint
注意是最少,这就要求必须是先进行排序,先把楼层高的送到,则结果是最少的楼层数。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int n = sc.nextInt();
int m = sc.nextInt();
int [] a = new int[n + 1];
int i, num = 0;
for(i = 1; i <= n; i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a);
while(n != 0) {
num += (a[n] - 1) * 2;
n -= m;
if(n < 0)
n = 0;
}
System.out.println(num);
}
}
}
1282

被折叠的 条评论
为什么被折叠?



