- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanoe.java
More file actions
Latest commit
44 lines (38 loc) · 1007 Bytes
/
Copy pathCanoe.java
File metadata and controls
44 lines (38 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
packagecom.test;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.Scanner;
publicclassCanoe {
/**
* @author wyl
* n个人,已知每个人体重。独木舟承重固定,每只独木舟最多坐两个人,
* 可以坐一个人或者两个人。显然要求总重量不超过独木舟承重,
* 假设每个人体重也不超过独木舟承重,问最少需要几只独木舟?
*/
publicstaticvoidmain(String[] args) {
ArrayList<Integer> list = newArrayList<Integer>();
Scannersc = newScanner(System.in);
inttmp;
intMAX=150; //船隻承受的重量
intsum=0; //需要的船隻數量
//輸入人的體重
for(inti=0;i<7;i++){ //這裡制定有7個人
tmp = sc.nextInt();
list.add(tmp);
}
Collections.sort(list);
//把人的體重排序
System.out.println(list.toString());
intlen = list.size();
System.out.println(len);
intj = 0;
while(j<len){
if(list.get(j)+list.get(len-1)<= MAX){
j++; //體重輕的出去
}
len--; //體重重的出去
sum++; //船隻數量增加
}
System.out.println("需要的船隻數量為--》"+sum);
}
}