Java实现贪心解决

漂流船问题

http://www.nowcoder.com/questionTerminal/0e6cb06ec63148ed952f887a787f0103

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

public class 漂流船问题 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] strings = sc.nextLine().split(" ");
int capacity = sc.nextInt();
int[] weights = new int[strings.length];
for (int i = 0;i<strings.length;i++){
weights[i] = Integer.parseInt(strings[i]);
}
Arrays.sort(weights);
int l = 0;
int r = weights.length-1;
int ans = 0;
while (l<r){
if (weights[l]+weights[r]<=capacity){
l++;
r--;
}else {
r--;
}
ans++;
}
if (l==r){
ans++;
}
System.out.println(ans);

}

}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-16 12:18
点赞 评论 收藏
分享
frutiger:逆天,我家就安阳的,这hr咋能说3k的,你送外卖不比这工资高得多?还说大厂来的6k,打发叫花子的呢?这hr是怎么做到说昧良心的话的
找工作时遇到的神仙HR
点赞 评论 收藏
分享
06-02 15:53
阳光学院 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-11 11:00
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务